简体   繁体   中英

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 .

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 .

How can I use Object.defineProperty to add get and set accessors on the name property in obj ?

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

When you get obj.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
When you get this.name , the get function is called, which returns the value of this.name .
Uncaught RangeError: Maximum call stack size exceeded.


Try return this.__name; and 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 . You've created a recursive function call that never exits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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