function dataWatch(data) {
let keyList = {}
let objHandler = {
get: function (target, propKey) {
return target[propKey]
},
set: function (target, propKey, value) {
if (Object.keys(keyList).indexOf(propKey) > -1) {
keyList[propKey](value)
}
target[propKey] = value
},
}
let objProxy = new Proxy(data, objHandler)
let operater = {
setData: function (newData) {
setTimeout(() => {
for (let key in newData) {
if (newData.hasOwnProperty(key)) {
objProxy[key] = newData[key]
}
}
})
},
watch: function (key, callback) {
keyList[key] = callback
},
}
operater.setData(data)
return operater
}
最后修改:2023 年 06 月 06 日
© 允许规范转载