代理对象情况
let o = new Proxy({ a: 1, b: 2 }, {
get(obj,prop) { return obj[prop]; },
set(obj,prop,newValue) {
document.querySelector('#data').innerText = newValue
val = newValue;
},
})
document.querySelector('#input').addEventListener('input', (e) => {
o.a = e.target.value
})
proxy 可以代理数组,完成响应式,这是defineproperty做不到的
let o = new Proxy([1,2,3,4], {
get(obj,prop) { return obj[prop]; },
set(obj,prop,newValue) {
console.log(obj,prop)
document.querySelector('#data').innerText = newValue
val = newValue;
},
})
document.querySelector('#input').addEventListener('input', (e) => {
o[3] = e.target.value
})
网友评论