Consider the following code:
var x = 0;
var o = {};
function getter() {
return x;
}
Object.defineProperty(o, "y", {
get: getter,
set: function (y) {
x = y;
Object.defineProperty(o, "y", {
get: getter
});
},
configurable: true
});
我的目标是去掉setter,并在setter被调用一次后使属性o.y
不可配置。但是它并没有按照预期工作:
> x // 0
> o.y // 0
> o.y = 1 // 1
> x // 1
> o.y // 1
> o.y = 2 // 2
> x // 2
> o.y // 2
因此,我的代码没有按预期工作,我无法想出其他解决方案。因此提出此问题。
nextProps
)不可修改的技巧。 - Marco Faustinelli