let data = {
username: "jxnuzhouguohong",
age: 39
}
// 模拟组件的实例
let _this = {};
// 利用Object.defineProperty()
for (let item in data) {
Object.defineProperty(_this, item, {
get() {
console.log("get()");
return data[item];
},
set(newValue) {
console.log("set()", newValue);
// 千万不要在set方法中修改当前扩展属性的值,会出现死循环
// _this.username = newValue;
data[item] = newValue;
}
})
}
console.log(_this);
// 通过Object.defineProperty的get方法添加的扩展属性不能直接对象.属性修改,但是会调用set方法,然后在set方法里面 data[item] = newValue;
_this.username = "just";
console.log(_this);
let just = {
name : "jxnuzhouguohong",
age : 39,
set info(name){
this.name = name;
},
get info(){
return this.name + this.age;
}
}
console.log(just);
javascript数据劫持代理
最新推荐文章于 2024-09-14 18:52:56 发布