vue源碼學(xué)習(xí)之Object.defineProperty 對數(shù)組監(jiān)聽
上一篇中,我們介紹了一下defineProperty 對對象的監(jiān)聽,這一篇我們看下defineProperty 對數(shù)組的監(jiān)聽
數(shù)組的變化
先讓我們了解下Object.defineProperty()對數(shù)組變化的跟蹤情況:
var a={};
bValue=1;
Object.defineProperty(a,"b",{
set:function(value){
bValue=value;
console.log("setted");
},
get:function(){
return bValue;
}
});
a.b;//1
a.b=[];//setted
a.b=[1,2,3];//setted
a.b[1]=10;//無輸出
a.b.push(4);//無輸出
a.b.length=5;//無輸出
a.b;//[1,10,3,4,undefined];
可以看到,當(dāng)a.b被設(shè)置為數(shù)組后,只要不是重新賦值一個新的數(shù)組對象,任何對數(shù)組內(nèi)部的修改都不會觸發(fā)setter方法的執(zhí)行。這一點非常重要,因為基于Object.defineProperty()方法的現(xiàn)代前端框架實現(xiàn)的數(shù)據(jù)雙向綁定也同樣無法識別這樣的數(shù)組變化。因此第一點,如果想要觸發(fā)數(shù)據(jù)雙向綁定,我們不要使用arr[1]=newValue;這樣的語句來實現(xiàn);第二點,框架也提供了許多方法來實現(xiàn)數(shù)組的雙向綁定。
對于框架如何實現(xiàn)數(shù)組變化的監(jiān)測,大多數(shù)情況下,框架會重寫Array.prototype.push方法,并生成一個新的數(shù)組賦值給數(shù)據(jù),這樣數(shù)據(jù)雙向綁定就會觸發(fā)。
實現(xiàn)簡單的對數(shù)組的變化的監(jiān)聽
var arrayPush = {};
(function(method){
var original = Array.prototype[method];
arrayPush[method] = function() {
// this 指向可通過下面的測試看出
console.log(this);
return original.apply(this, arguments)
};
})('push');
var testPush = [];
testPush.__proto__ = arrayPush;
// 通過輸出,可以看出上面所述 this 指向的是 testPush
// []
testPush.push(1);
// [1]
testPush.push(2);
在官方文檔,所需監(jiān)視的只有 push()、pop()、shift()、unshift()、splice()、sort()、reverse() 7 種方法。我們可以遍歷一下:
var arrayProto = Array.prototype
var arrayMethods = Object.create(arrayProto)
;[
'push',
'pop',
'shift',
'unshift',
'splice',
'sort',
'reverse'
].forEach(function(item){
Object.defineProperty(arrayMethods,item,{
value:function mutator(){
//緩存原生方法,之后調(diào)用
console.log('array被訪問');
var original = arrayProto[item]
var args = Array.from(arguments)
original.apply(this,args)
// console.log(this);
},
})
})
完整代碼
function Observer(data){
this.data = data;
this.walk(data);
}
var p = Observer.prototype;
var arrayProto = Array.prototype
var arrayMethods = Object.create(arrayProto)
;[
'push',
'pop',
'shift',
'unshift',
'splice',
'sort',
'reverse'
].forEach(function(item){
Object.defineProperty(arrayMethods,item,{
value:function mutator(){
//緩存原生方法,之后調(diào)用
console.log('array被訪問');
var original = arrayProto[item]
var args = Array.from(arguments)
original.apply(this,args)
// console.log(this);
},
})
})
p.walk = function(obj){
var value;
for(var key in obj){
// 通過 hasOwnProperty 過濾掉一個對象本身擁有的屬性
if(obj.hasOwnProperty(key)){
value = obj[key];
// 遞歸調(diào)用 循環(huán)所有對象出來
if(typeof value === 'object'){
if (Array.isArray(value)) {
var augment = value.__proto__ ? protoAugment : copyAugment
augment(value, arrayMethods, key)
observeArray(value)
}
new Observer(value);
}
this.convert(key, value);
}
}
};
p.convert = function(key, value){
Object.defineProperty(this.data, key, {
enumerable: true,
configurable: true,
get: function(){
console.log(key + '被訪問');
return value;
},
set: function(newVal){
console.log(key + '被修改,新' + key + '=' + newVal);
if(newVal === value) return ;
value = newVal;
}
})
};
var data = {
user: {
// name: 'zhangsan',
age: function(){console.log(1)}
},
apg: [{'a': 'b'},2,3]
}
function observeArray (items) {
for (var i = 0, l = items.length; i < l; i++) {
observe(items[i])
}
}
//數(shù)據(jù)重復(fù)Observer
function observe(value){
if(typeof(value) != 'object' ) return;
var ob = new Observer(value)
return ob;
}
//輔助方法
function def (obj, key, val) {
Object.defineProperty(obj, key, {
value: val,
enumerable: true,
writable: true,
configurable: true
})
}
// 兼容不支持__proto__的方法
//重新賦值A(chǔ)rray的__proto__屬性
function protoAugment (target,src) {
target.__proto__ = src
}
//不支持__proto__的直接修改相關(guān)屬性方法
function copyAugment (target, src, keys) {
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i]
def(target, key, src[key])
}
}
var app = new Observer(data);
// data.apg[2] = 111;
data.apg.push(5);
// data.apg[0].a = 10;
// console.log(data.apg);
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3+Element?Plus按需引入(自動導(dǎo)入)詳解
element-plus根據(jù)官網(wǎng)文檔,推薦用戶采用按需導(dǎo)入的方式進行導(dǎo)入,下面這篇文章主要給大家介紹了關(guān)于Vue3+Element?Plus按需引入(自動導(dǎo)入)的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10
Element?ui關(guān)閉el-dialog時如何清除數(shù)據(jù)
這篇文章主要介紹了Element?ui關(guān)閉el-dialog時如何清除數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
Vue 中使用vue2-highcharts實現(xiàn)top功能的示例
下面小編就為大家分享一篇Vue 中使用vue2-highcharts實現(xiàn)top功能的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03

