# vue 双向绑定实现
# es5 的实现
Object.defineProperty(obj, key, {
get: function reactiveGetter() {
return val;
},
set: function reactiveSetter(newVal) {
val = newVal;
// 改变页面内容的操作
h1.innerText = val;
},
}
# es6 的实现
let modal = { title: "" };
modal = new Proxy(modal, {
get(target, p) {
return target[p];
},
set(target, p, value) {
target[p] = value;
// 改变页面内容的操作
h1.innerText = val;
},
});