生活随笔
收集整理的這篇文章主要介紹了
vue --- 2.0响应式补充
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
補充:
let odlArrayPrototype
= Array
.prototype
;
let proto
= Object
.create(odlArrayPrototype
);
['push','shift','unshift'].forEach(method
=>{proto
[method
] = function(){ updateView(); odlArrayPrototype
[method
].call(this, ...arguments
);}
})function updateView() {console
.log('更新視圖');
}function observer(target
) {if (typeof target
!== 'object' || target
=== null) {return target
;}if(Array
.isArray(target
)){ target
.__proto__
= proto
;}Object
.keys(target
).forEach(key
=>{defineReactive(target
, key
, target
[key
]);})
}function defineReactive(obj
, key
, value
){observer(value
);Object
.defineProperty(obj
,key
,{get(){return value
},set(newVal
){if(newVal
!== value
){observer(newVal
);updateView();value
= newVal
;}}})
}
let data
= {name
:'lz', age
:[1,2,3]};
observer(data
);
data
.age
.push(4);
data
.age
.push(5);
總結(jié)
以上是生活随笔為你收集整理的vue --- 2.0响应式补充的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。