Element.prototype.remove - 关闭编译器警告

Element.prototype.remove - Closure Compiler warning

我正在使用这个解决方案:(来自:Remove element by id

Element.prototype.remove = function() {
    this.parentElement.removeChild(this);
}
NodeList.prototype.remove = HTMLCollection.prototype.remove = function() {
    for(var i = 0, len = this.length; i < len; i++) {
        if(this[i] && this[i].parentElement) {
            this[i].parentElement.removeChild(this[i]);
        }
    }
}

我在闭包编译器中收到以下警告:

externs.zip//w3c_dom2.js:793: WARNING - mismatch of the remove property type and the type of the property it overrides from superclass Element original: function (this:Element): undefined override: function (this:HTMLSelectElement, number): undefined HTMLSelectElement.prototype.remove = function(index) {};

如何清除此警告?或者我应该使用另一种方法 element.remove?

编译器警告您,从 Element 继承的 class 已经有一个名为 remove 的方法,并且它的签名与您的不匹配。在这种情况下,HTMLSelectElement.

如果您忽略该警告,您的删除方法将无法对 HTMLSelectElement 元素正常运行 - 因为该函数执行的行为与您定义的行为截然不同。

@php_nub_qq 的评论是正确的。选择一个与继承链中现有对象不冲突的方法名。