对象为 getter 和 setter 定义 属性
Object define property for getter and setter
我收到有关此代码的最大调用堆栈大小的错误。
function ValueObject() {
}
ValueObject.prototype.authentication;
Object.defineProperty(ValueObject.prototype, "authentication", {
get : function () {
return this.authentication;
},
set : function (val) {
this.authentication = val;
}
});
var vo = new ValueObject({last: "ac"});
vo.authentication = {a: "b"};
console.log(vo);
错误
RangeError: Maximum call stack size exceeded
那是因为每次赋值发生时都会执行set
函数。您正在定义递归代码。如果您定义了 authentication
以外的其他 属性,则不会出现该错误。
Object.defineProperty(ValueObject.prototype, "authentication", {
get : function () {
return this._authentication;
},
set : function (val) {
this._authentication = val;
}
});
我收到有关此代码的最大调用堆栈大小的错误。
function ValueObject() {
}
ValueObject.prototype.authentication;
Object.defineProperty(ValueObject.prototype, "authentication", {
get : function () {
return this.authentication;
},
set : function (val) {
this.authentication = val;
}
});
var vo = new ValueObject({last: "ac"});
vo.authentication = {a: "b"};
console.log(vo);
错误
RangeError: Maximum call stack size exceeded
那是因为每次赋值发生时都会执行set
函数。您正在定义递归代码。如果您定义了 authentication
以外的其他 属性,则不会出现该错误。
Object.defineProperty(ValueObject.prototype, "authentication", {
get : function () {
return this._authentication;
},
set : function (val) {
this._authentication = val;
}
});