/*
* not type checking this file because flow doesn't play well with
* dynamically accessing methods on Array prototype
*/import{ def }from'../util/index'//让arrayProto 等于了 数组原型const arrayProto = Array.prototype
exportconst arrayMethods = Object.create(arrayProto)const methodsToPatch =['push','pop','shift','unshift','splice','sort','reverse']/**
* Intercept mutating methods and emit events
*/
methodsToPatch.forEach(function(method){// cache original method// //调用数组原型上的方法const original = arrayProto[method]def(arrayMethods, method,functionmutator(...args){const result = original.apply(this, args)const ob =this.__ob__
let inserted
switch(method){case'push':case'unshift':
inserted = args
breakcase'splice':
inserted = args.slice(2)break}if(inserted) ob.observeArray(inserted)// notify change// 调用nofify通知改变了
ob.dep.notify()//改变后的结果返回出去return result
})})
理解使用数据劫持的方式,重写了数组的方法vue将data中的数组进行了原型链的重写,指向了自定义的数组原型方法这样当调用数组api时,可以通知依赖更新,如果数组中包含着引用类型,对数组中的引用类型再次进行监控vue-array源码/* * not type checking this file because flow doesn't play well with * dynamically accessing methods on Array prototype */impor