Search code examples
javascriptprototypejs

TypeError: property Array.prototype.splice.call(...) is non-configurable and can't be deleted


When I attempt to load my page in FF I get this error:

TypeError: property Array.prototype.splice.call(...) is non-configurable and can't be deleted

Here is the prototype

   HTMLElement.prototype.selectorAll = function (selectors, fun) {

        var sels = Array.prototype.splice.call(this.querySelectorAll(selectors), 0)
        if (!fun) { return sels; }; fun.call(sels);
    };

How do I fix this error?


Solution

  • Use slice rather than splice to just create a new Array from the original collection.

    var sels = Array.prototype.slice.call(this.querySelectorAll(selectors), 0)
    

    The error is because splice is also attempting to modify the original collection:

    var a = [ 1, 2, 3, 4 ];
    
    a.slice(0);
    console.log(a); // [ 1, 2, 3, 4 ]
    
    a.splice(0);
    console.log(a); // []
    

    And the NodeList returned from querySelectorAll() has a non-configurable property that splice can't alter as it expects.