[英]Object define property for getter and setter
I'm getting an error on Maximum call stack size for this code. 我在此代码的最大调用堆栈大小上遇到错误。
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);
Error 错误
RangeError: Maximum call stack size exceeded
That's because the set
function is executed each time the assignment happens. 这是因为每次分配发生时都会执行
set
函数。 You are defining a recursive code. 您正在定义一个递归代码。 If you define a different property other than
authentication
then you don't get that error. 如果您定义了
authentication
以外的其他属性,则不会收到该错误。
Object.defineProperty(ValueObject.prototype, "authentication", {
get : function () {
return this._authentication;
},
set : function (val) {
this._authentication = val;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.