[英]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
上有一个get
和set
访问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.defineProperty
在obj
的name
属性上添加get
和set
访问器?
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.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用get
函数,它返回this.name
的值。
When you get this.name
, the get
function is called, which returns the value of this.name
. 当你得到
this.name
, get
调用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.