1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| var data = { name:'wsw', info:[ 27, {sex:1} ] } function observe(data){ if(Array.isArray(data)){ data.__proto__ = arrayMethods; this.observerArray(data) }else{ walk(data); } } function observerArray(value){ for(let i = 0; i< value.length;i++){ observe(value[i]) } } function walk(data){ let keys = Object.keys(data); keys.forEach((key) => { defineReactive(data,key,data[key]) }) } function defineReactive(data,key,value) { observe(value) Object.defineProperty(data,key,{ get() { return value }, set(newV) { if(newV == value)return; console.log("数据修改") observe(newV) value = newV; } }) }
|