简体   繁体   English

在RangeError中使用`Object.defineProperty`

[英]Using `Object.defineProperty` with RangeError

I'm trying to use Object.defineProperty to update my obj object to have a get and set accessor on obj.name . 我正在尝试使用Object.defineProperty来更新我的obj对象,以便在obj.name上有一个getset访问obj.name

var obj = {};

Object.defineProperty(obj, 'name', { 
    get: function() { return this.name; },
    set: function(x) { this.name = x; }
});

console.log("obj:", obj);
console.log("obj.name:", obj.name);

But I'm getting a Uncaught RangeError: Maximum call stack size exceeded . 但是我得到了一个Uncaught RangeError: Maximum call stack size exceeded

How can I use Object.defineProperty to add get and set accessors on the name property in obj ? 如何使用Object.definePropertyobjname属性上添加getset访问器?

http://jsfiddle.net/kman007_us/ZwYp6/ http://jsfiddle.net/kman007_us/ZwYp6/

When you get obj.name , the get function is called, which returns the value of this.name . 当你得到obj.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
When you get this.name , the get function is called, which returns the value of this.name . 当你得到this.nameget调用get函数,它返回this.name的值。
Uncaught RangeError: Maximum call stack size exceeded. 未捕获RangeError:超出最大调用堆栈大小。


Try return this.__name; 尝试return this.__name; and this.__name = x; this.__name = x; instead. 代替。

You cannot access a property in the getters or setters by the same name as a property you are defining using Object.defineProperty . 您无法使用与使用Object.defineProperty定义的属性相同的名称访问getter或setter中的属性。 You've created a recursive function call that never exits. 您创建了一个永不退出的递归函数调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM