TypeScript:get/set,引用自身和基础值
TypeScript: get/set, referring to itself and underlying values
这个特定的 getter/setter 在 TypeScript 中是如何工作的:
export class MyClass {
get prop() {
return this.prop;
}
set prop(val: string) {
this.prop = val;
}
}
getter和setter都是指this.prop
,但它们都定义了this.prop
。那么它指向的实际潜在价值是什么?
在翻译的这段代码中,生成的这段ES2015代码是:
var MyClass = /** @class */ (function () {
function MyClass() {
}
Object.defineProperty(MyClass.prototype, "prop", {
get: function () {
return this.prop;
},
set: function (val) {
this.prop = val;
},
enumerable: true,
configurable: true
});
return MyClass;
}());
如果我将 getter/setter 更改为引用局部变量,如下所示:
export class MyClass {
private _prop;
get prop() {
return this._prop;
}
set prop(val: string) {
this._prop = val;
}
}
它生成这个:
Object.defineProperty(exports, "__esModule", { value: true });
var MyClass = /** @class */ (function () {
function MyClass() {
}
Object.defineProperty(MyClass.prototype, "prop", {
get: function () {
return this._prop;
},
set: function (val) {
this._prop = val;
},
enumerable: true,
configurable: true
});
return MyClass;
}());
期望在某处看到 var _prop
定义,但无处可见(除了返回值并设置它)!我错过了一个把戏吗?还是隐式定义?
这是 ES2015/Typescript 中的 属性 访问器语法。
用 get
定义的 getter 定义了如何检索 属性 值,并将在需要 属性 值时调用(例如:console.log(obj.prop);
将调用 getter)。
用set
定义的setter定义了如何在对象上保存属性值,并在属性的值被赋值时调用(例如: this.prop = "Test"
)
您可以阅读更多相关内容here
继续看你的例子,第一个是逻辑错误,会导致堆栈溢出异常,因为在你的 getter 中,你使用相同的 属性,因此调用 getter 和 setter 一样。很像这在语法上是有效的,但不会起作用:
export class MyClass {
method(){
this.method();
}
}
第二个示例定义了一个私有字段但没有对其进行初始化。由于没有初始化,因此无需发出任何代码来定义 _prop
字段,它将在 javascript 中首次分配时创建。如果您已经初始化了该字段,它就会显示在构造函数中:
export class MyClass {
private _prop = "";
}
会产生:
var MyClass = /** @class */ (function () {
function MyClass() {
this._prop = "";
}
}
这个特定的 getter/setter 在 TypeScript 中是如何工作的:
export class MyClass {
get prop() {
return this.prop;
}
set prop(val: string) {
this.prop = val;
}
}
getter和setter都是指this.prop
,但它们都定义了this.prop
。那么它指向的实际潜在价值是什么?
在翻译的这段代码中,生成的这段ES2015代码是:
var MyClass = /** @class */ (function () {
function MyClass() {
}
Object.defineProperty(MyClass.prototype, "prop", {
get: function () {
return this.prop;
},
set: function (val) {
this.prop = val;
},
enumerable: true,
configurable: true
});
return MyClass;
}());
如果我将 getter/setter 更改为引用局部变量,如下所示:
export class MyClass {
private _prop;
get prop() {
return this._prop;
}
set prop(val: string) {
this._prop = val;
}
}
它生成这个:
Object.defineProperty(exports, "__esModule", { value: true });
var MyClass = /** @class */ (function () {
function MyClass() {
}
Object.defineProperty(MyClass.prototype, "prop", {
get: function () {
return this._prop;
},
set: function (val) {
this._prop = val;
},
enumerable: true,
configurable: true
});
return MyClass;
}());
期望在某处看到 var _prop
定义,但无处可见(除了返回值并设置它)!我错过了一个把戏吗?还是隐式定义?
这是 ES2015/Typescript 中的 属性 访问器语法。
用 get
定义的 getter 定义了如何检索 属性 值,并将在需要 属性 值时调用(例如:console.log(obj.prop);
将调用 getter)。
用set
定义的setter定义了如何在对象上保存属性值,并在属性的值被赋值时调用(例如: this.prop = "Test"
)
您可以阅读更多相关内容here
继续看你的例子,第一个是逻辑错误,会导致堆栈溢出异常,因为在你的 getter 中,你使用相同的 属性,因此调用 getter 和 setter 一样。很像这在语法上是有效的,但不会起作用:
export class MyClass {
method(){
this.method();
}
}
第二个示例定义了一个私有字段但没有对其进行初始化。由于没有初始化,因此无需发出任何代码来定义 _prop
字段,它将在 javascript 中首次分配时创建。如果您已经初始化了该字段,它就会显示在构造函数中:
export class MyClass {
private _prop = "";
}
会产生:
var MyClass = /** @class */ (function () {
function MyClass() {
this._prop = "";
}
}