0

我的个人库中有几个定义,这个在 Safari 中生成错误:

Object.defineProperty(Element.prototype, "remove", {
    enumerable: false,
    configurable: false,
    writable: false,
    value: function(){
        this.parentElement.removeChild(this);
    }
});

错误:TypeError: Attempting to change enumerable attribute of unconfigurable property。

我不明白这句话的确切含义,在 FireFox 中不会发生此错误。

4

1 回答 1

0

消息说:

1) Element.prototype 已经有了属性remove

2) 该configurable属性的 - 属性设置为false。这意味着:该属性remove不能以任何方式更改。

要评估该尝试:

Object.getOwnPropertyDescriptor(Element.prototype, 'remove');

有些浏览器没有removeon Element.prototype,那么您的代码可以工作。某些浏览器具有并configurable设置为true,然后它也可以工作,并且您会覆盖内置属性。

顺便说一句:属性enumerableconfigurablewritable默认为false,您必须仅在希望它们为 时声明它们true

第二个旁白:在内置原型中乱搞并不是一个好主意。

最后一点:我的 Safari (5.1.7) 不再有 Element.prototype.remove,而且您的代码可以正常工作。

于 2014-09-24T12:48:01.103 回答