我试图创建一个属性的获取和设置方法:
private _name: string;
Name() {
get:
{
return this._name;
}
set:
{
this._name = ???;
}
}
设置值的关键字是什么?
我试图创建一个属性的获取和设置方法:
private _name: string;
Name() {
get:
{
return this._name;
}
set:
{
this._name = ???;
}
}
设置值的关键字是什么?
当前回答
如果你正在寻找在任何对象(不是类)上使用get和set的方法,代理可能是有用的: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
const target = {
message1: "hello",
message2: "everyone"
};
const handler3 = {
get: function (target, prop, receiver) {
if (prop === "message2") {
return "world";
}
return Reflect.get(...arguments);
},
};
const proxy3 = new Proxy(target, handler3);
console.log(proxy3.message1); // hello
console.log(proxy3.message2); // world
注意:请注意,这是不支持的新api,旧浏览器需要polifill
其他回答
我想我大概知道为什么这么令人困惑了。在您的示例中,我们需要_name的getter和setter。但我们通过为不相关的类变量Name创建getter和setter来实现这一点。
考虑一下:
class Car {
private tiresCount = 4;
get yourCarTiresCount(){
return this.tiresCount;
}
set yourCarTiresCount(count) {
alert('You shouldn\'t change car tire count')
}
}
上面的代码如下:
get和set为yourCarTiresCount创建getter和setter(不是为tiresCount)。
getter是:
function () {
return this.tiresCount;
}
setter是:
function (count) {
alert('You shouldn\'t change car tire count');
}
意思是,每次执行new Car()。yourCarTiresCount, getter运行。并且对于每一个新的Car(). yourcartirescount ('7') setter运行。
间接地为私有ti复述创建getter,而不是setter。
TypeScript使用getter/setter语法,类似于ECMAScript4/ActionScript3。
class foo {
private _bar: boolean = false;
get bar(): boolean {
return this._bar;
}
set bar(value: boolean) {
this._bar = value;
}
}
然而,为了使用它,你必须确保TypeScript编译器的目标是ECMAScript5或更高。如果你正在运行命令行编译器,使用——target标志,像这样;
tsc --target ES5
如果你正在使用Visual Studio,你必须编辑你的项目文件,为TypeScriptCompile构建工具的配置添加标志。你可以在这里看到:
这将使用ECMAScript 5 Object.defineProperty()特性生成这个JavaScript。
var foo = (function () {
function foo() {
this._bar = false;
}
Object.defineProperty(foo.prototype, "bar", {
get: function () {
return this._bar;
},
set: function (value) {
this._bar = value;
},
enumerable: true,
configurable: true
});
return foo;
})();
最新版本的EcmaScript将生成更像原始TypeScript的代码。例如,针对EcmaScript2017将产生:
"use strict";
class foo {
constructor() {
this._bar = false;
}
get bar() {
return this._bar;
}
set bar(value) {
this._bar = value;
}
}
所以要使用它,
var myFoo = new foo();
if(myFoo.bar) { // calls the getter
myFoo.bar = false; // calls the setter and passes false
}
正如下面@DanFromGermany所建议的,如果你只是读写一个本地属性,比如foo。Bar = true,则setter和getter对是多余的。如果以后需要做一些事情,比如在读取或写入属性时,您总是可以添加它们。
getter可以用来实现只读属性。下面的示例还展示了getter如何与只读类型和可选类型交互。
//
// type with optional readonly property.
// baz?:string is the same as baz:string|undefined
//
type Foo = {
readonly bar: string;
readonly baz?: string;
}
const foo:Foo = {bar: "bar"}
console.log(foo.bar) // prints 'bar'
console.log(foo.baz) // prints undefined
//
// interface with optional readonly property
//
interface iFoo {
readonly bar: string;
readonly baz?: string;
}
const ifoo:iFoo = {bar: "bar"}
console.log(ifoo.bar) // prints 'bar'
console.log(ifoo.baz) // prints undefined
//
// class implements bar as a getter,
// but leaves off baz.
//
class iBarClass implements iFoo {
get bar() { return "bar" }
}
const iBarInstance = new iBarClass()
console.log(iBarInstance.bar) // prints 'bar'
console.log(iBarInstance.baz) // prints 'undefined'
// accessing baz gives warning that baz does not exist
// on iBarClass but returns undefined
// note that you could define baz as a getter
// and just return undefined to remove the warning.
//
// class implements optional readonly property as a getter
//
class iBazClass extends iBarClass {
private readonly _baz?: string
constructor(baz?:string) {
super()
this._baz = baz
}
get baz() { return this._baz; }
}
const iBazInstance = new iBazClass("baz")
console.log(iBazInstance.bar) // prints bar
console.log(iBazInstance.baz) // prints baz
它非常类似于创建常用方法,只需在开头放置关键字reserved get或set。
class Name{
private _name: string;
getMethod(): string{
return this._name;
}
setMethod(value: string){
this._name = value
}
get getMethod1(): string{
return this._name;
}
set setMethod1(value: string){
this._name = value
}
}
class HelloWorld {
public static main(){
let test = new Name();
test.setMethod('test.getMethod() --- need ()');
console.log(test.getMethod());
test.setMethod1 = 'test.getMethod1 --- no need (), and used = for set ';
console.log(test.getMethod1);
}
}
HelloWorld.main();
在这种情况下,你可以在get getMethod1(){中跳过返回类型
get getMethod1() {
return this._name;
}
Ezward已经提供了一个很好的答案,但我注意到其中一个评论问它是如何使用的。对于像我这样偶然遇到这个问题的人,我认为在Typescript网站上有一个关于getter和setter的官方文档的链接会很有用,因为它能很好地解释它,并希望在发生更改时始终保持最新,并显示示例用法:
http://www.typescriptlang.org/docs/handbook/classes.html
特别地,对于那些不熟悉它的人,请注意,在调用getter时不要合并'get'这个词(setter也是如此):
var myBar = myFoo.getBar(); // wrong
var myBar = myFoo.get('bar'); // wrong
你应该简单地这样做:
var myBar = myFoo.bar; // correct (get)
myFoo.bar = true; // correct (set) (false is correct too obviously!)
给定一个类:
class foo {
private _bar:boolean = false;
get bar():boolean {
return this._bar;
}
set bar(theBar:boolean) {
this._bar = theBar;
}
}
然后私有属性'_bar'的'bar' getter将被调用。
如果你正在使用TypeScript模块,并试图添加一个导出的getter,你可以这样做:
// dataStore.ts
export const myData: string = undefined; // just for typing support
let _myData: string; // for memoizing the getter results
Object.defineProperty(this, "myData", {
get: (): string => {
if (_myData === undefined) {
_myData = "my data"; // pretend this took a long time
}
return _myData;
},
});
然后,在另一个文件中:
import * as dataStore from "./dataStore"
console.log(dataStore.myData); // "my data"