Fiddle如何撤消Object.defineProperty调用?
var Assertion = function() {
return { "dummy": "data" };
}
Object.defineProperty(Object.prototype, 'should', {
set: function(){},
get: function(){
return new Assertion(this);
}
});
// Insert magic here.
// This needs to be false
console.log(({}).should === undefined);
我有什么选择在ES5撤消defineProperty
电话吗?
请不要愚蠢的建议,如Object.defineProperty = function() { }
。
以下Object.defineProperty(Object.prototype, 'should', {})
确实not work
和Object.defineProperty(Object.prototype, 'should', { value: undefined })
抛出在V8 Uncaught TypeError: Cannot redefine property: defineProperty
Object.defineProperty(Object.prototype, 'should', {
set: function() {},
get: function() { return undefined; }
});
delete Object.prototype.should
也does not work
任何人都可以解释为什么我们不能通过赋予像这样的属性的新值来覆盖或移除一个'accessor'; obj.foo ='new value';'?换句话说,为什么我们必须首先删除属性或重新配置它以摆脱该访问器 – kapreski