Object.defineproperty实现的响应式
对于对象的响应式
是给对象的每个属性单独设置一个getter和setter来收集和触发依赖于其的数据,比较低效
对于数组的响应式
其实vue2的响应式对于数组就做了两件事
① 对数组已有元素(索引 0、1、2...)做 defineProperty 拦截;(在 Vue 2 的响应式系统中,对数组的元素也是通过 逐个调用 Object.defineProperty 来添加 getter/setter 拦截的。)
②替换掉 Array.prototype 上的方法(如 push/splice),通过改写原型来拦截变更操作。