十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
怎么在vue中利用Object.defineProperty 對(duì)數(shù)組進(jìn)行監(jiān)聽(tīng)?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
10年積累的網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先建設(shè)網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有南州晴隆免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
數(shù)組的變化
先讓我們了解下Object.defineProperty()對(duì)數(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;//無(wú)輸出 a.b.push(4);//無(wú)輸出 a.b.length=5;//無(wú)輸出 a.b;//[1,10,3,4,undefined];
可以看到,當(dāng)a.b被設(shè)置為數(shù)組后,只要不是重新賦值一個(gè)新的數(shù)組對(duì)象,任何對(duì)數(shù)組內(nèi)部的修改都不會(huì)觸發(fā)setter方法的執(zhí)行。這一點(diǎn)非常重要,因?yàn)榛贠bject.defineProperty()方法的現(xiàn)代前端框架實(shí)現(xiàn)的數(shù)據(jù)雙向綁定也同樣無(wú)法識(shí)別這樣的數(shù)組變化。因此第一點(diǎn),如果想要觸發(fā)數(shù)據(jù)雙向綁定,我們不要使用arr[1]=newValue;這樣的語(yǔ)句來(lái)實(shí)現(xiàn);第二點(diǎn),框架也提供了許多方法來(lái)實(shí)現(xiàn)數(shù)組的雙向綁定。
對(duì)于框架如何實(shí)現(xiàn)數(shù)組變化的監(jiān)測(cè),大多數(shù)情況下,框架會(huì)重寫Array.prototype.push方法,并生成一個(gè)新的數(shù)組賦值給數(shù)據(jù),這樣數(shù)據(jù)雙向綁定就會(huì)觸發(fā)。
實(shí)現(xiàn)簡(jiǎn)單的對(duì)數(shù)組的變化的監(jiān)聽(tīng)
var arrayPush = {}; (function(method){ var original = Array.prototype[method]; arrayPush[method] = function() { // this 指向可通過(guò)下面的測(cè)試看出 console.log(this); return original.apply(this, arguments) }; })('push'); var testPush = []; testPush.__proto__ = arrayPush; // 通過(guò)輸出,可以看出上面所述 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被訪問(wèn)'); 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被訪問(wèn)'); 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){ // 通過(guò) hasOwnProperty 過(guò)濾掉一個(gè)對(duì)象本身?yè)碛械膶傩?nbsp; if(obj.hasOwnProperty(key)){ value = obj[key]; // 遞歸調(diào)用 循環(huán)所有對(duì)象出來(lái) 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 + '被訪問(wèn)'); 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)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。