vue数组变异

1.让一个变量继承数组的方法。

var OldArray=Array.prototype;

var newArray=Object.create(OldArray)

2.重写该变量的7种变异方法

 const mutationMethods = [
  'push',
  'pop',
  'shift',
  'unshift',
  'splice',
  'sort',
  'reverse'
]
      
mutationMethods.forEach((item)=>{
          let oldFun =OldArray[item]
        //  console.log(oldFun)
          newArray[item]=function(...args){
            //   console.log(this,item)
            let result=  oldFun.apply(this,args)
           
            return result;
          }
      })

3.让数组隐式继承新变量的方法

     let arr = []
// 通过隐式原型继承arrayMethods
    arr.__proto__ = newArray

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理