繁体   English   中英

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

[英]Element.prototype.remove - Closure Compiler warning

我正在使用此解决方案:(来自: 通过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继承的类已经具有名为remove的方法,并且其签名与您的签名不匹配。 在这种情况下, HTMLSelectElement

如果您忽略该警告,那么您的remove方法将无法正确用于HTMLSelectElement元素-因为该函数执行的行为与您定义的行为完全不同。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM