我正在尝试创建一个对象。 但我不明白为什么我的属性获取器设置器不能简单地调用 this.bar
。 因此,我的 foo 对象似乎有两个属性。
这是正确的,还是我:
- using defineProperties wrong
- missing the point
创建 foo
, 带有 bar
属性
var foo = function ()
{
Object.defineProperties(this, {
bar : {
get : function () {return this.barVal},
set : function(value) { this.barVal = value},
enumerable: true,
configurable: true
}
})
};
var o = new foo();
o.bar = "Hello";
console.log(JSON.stringify(o));
//output {"bar":"Hello","barVal":"Hello"}