<script>
let data = {
name:"尚硅谷",
address:"北京"
}
//创建一个监听实力
const obj = new Observer(data);
console.log(obj);
//准备一个vm 实例对象
let vm = {};
vm._data = data = obj;
console.log(vm);
function Observer(obj) {
//汇总对象中的属性key
const keys = Object.keys(obj);
keys.forEach((k) => {
Object.defineProperty(this,k,{
set(val){
console.log("设置数据");
obj[k] = val;
},
get(){
console.log("获取数据");
return obj[k]
}
});
})
}
</script>
网友评论