如何使只读属性在Javascript中变为可写?

时间:2015-11-08 15:05:33

标签: javascript reflection google-chrome-extension properties prototype

我试图让对象的属性可写。 它是一个复杂的第三方对象,所以我无法为它构建一个构造函数。

到目前为止,我已尝试过这样的事情。还有其他方法可以实现吗?

我试图替换原型中的属性,并为其创建一个包装器,但是" setter"在运行时未定义。

var originalProperty = Object.getOwnPropertyDescriptor(TheObjectClass.prototype, 'theProperty');                

delete TheObjectClass.prototype.theProperty;        

//the following is pseudo(is it?) code: 
Object.defineProperty(TheObjectClass.prototype, 'theProperty', {
    get: function() {return originalProperty.get.call(this);},
    set: function(val) {originalProperty.set.call(this,val);}
});

有没有人对如何实现这个想法有一个有趣的想法?

顺便说一句,如果可以隔离这样的财产,那么使用"呼叫"是否正确?或"申请"?什么是正确的方法?

PS:如果重要的话,我会在Chrome扩展程序中尝试这一点。

0 个答案:

没有答案