2

I am using this solution: (from: 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]);
        }
    }
}

I am getting the following warning in closure compiler:

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) {};

How can I clear this warning? Or should I use another approach for element.remove?

Community
  • 1
  • 1
sanchez
  • 4,519
  • 3
  • 23
  • 53

1 Answers1

3

The compiler is warning you that a class which inherits from Element already has a method named remove and that it's signature doesn't match yours. In this case, the HTMLSelectElement.

Were you to ignore the warning, your remove method would not function correctly for HTMLSelectElement elements - as that function performs a very different behavior from what you defined.

The comment by @php_nub_qq is correct. Choose a method name that does not conflict with existing objects in the inheritance chain.

Chad Killingsworth
  • 14,360
  • 2
  • 34
  • 57
  • 1
    Notice that `HTMLSelectElement::remove` is overloaded - if called without an index, it is indeed equivalent to [`Node::remove`](https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove). Polyfillying that should respect this of course, but using a different method name is not really an option. I wonder how GCC can deal with overloaded methods though. – Bergi Apr 21 '15 at 14:25
  • 2
    I had missed the new `ChildNode` interface. Closure-compiler doesn't currently recognize/implement that interface. I'll see about getting this worked in. That will resolve the issue. – Chad Killingsworth Apr 21 '15 at 15:08