TypeError: 属性 Array.prototype.splice.call(..) 是不可配置的,无法删除

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

本文关键字:配置 删除 Array 属性 prototype splice call TypeError      更新时间:2023-09-26

当我尝试在FF中加载我的页面时,出现此错误:

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

这是原型

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

如何修复此错误?

使用slice而不是splice从原始集合创建新Array

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

此错误是因为splice还尝试修改原始集合:

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

querySelectorAll()返回的NodeList具有不可配置的属性,splice无法按预期进行更改。