vue工程里面,对Array原型进行扩展时遇到报错

为了方便数组删除元素,我对array原型进行扩展


Array.prototype.removeElement=function (obj) {
  let index = this.indexOf(obj)
  if(index!==-1){
    this.splice(index,1)
  }
};

这样我可以通过元素进行删除,我试了一下,删除的时候没有问题,但是我发现在我用async/await 的时候,源码发现报错,意思应该是for in遍历到了我的这个自定义方法

img

在网上找的sync和await例子,然后把你写的数组删除元素放了进去,没报错。会不会是你的代码其它部分引起的这个错误。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    Array.prototype.removeElement=function (obj) {
        let index = this.indexOf(obj)
        if(index!==-1){
            this.splice(index,1)
        }
    };

    async function test1() {
        let arr = [1, 23, 43]
        arr.removeElement(23)
        console.warn(arr)
        console.log(await test2());
    }
    async function test2() {
        return await 'return test2 value'
    }
    test1();
    setTimeout(() => {
        console.log('setTimeout');
    }, 0);
    new Promise((resolve, reject) => {
        console.log('promise1');
        resolve();
    }).then(() => {
        console.log('promise2');
    });
    console.log('end async');
</script>
</body>
</html>

img

能贴一下完整的代码吗?

 Array.prototype.remove=function(element){
             for(i=0;i<this.length;i++){//遍历数组对象的元素
                if(this[i]==element){  //当相等时
                   this.splice(i,1);   //调用javascript的内置方法splice(规定要删除的项目的位置,要删除的项目数量),返回删除后的this。
                }
             }
             return this;   //返回调用remove方法的对象
        }



Array.prototype.remove = function (val) {
var index = this.indexOf(val);
if (index > -1) {
this.splice(index, 1);
}
};
这个是我在项目中用到的,没有报错你试试