87,904
社区成员
发帖
与我相关
我的任务
分享
let a = {};
a.b;
//此时的a应为{"b":true}
let a = new Proxy({}, {
get: (target, name)=> name in target ? target[name] : true
});
let a = {};
a.b;
//此时的a应为{"b":true}
a.c;
//此时的a应为{"b":true,"c":true}
也就是说事前是不知道属性名的,也就没法事先写对应的get和set。
to 3楼的兄弟:你想的太简单了,我需要的是的调用a.b的时候你那行代码的逻辑已经隐式执行了。
a.b == undefined ? a.b = true : a.b
var o = { get b() { return this.B===undefined?true:this.B; },set b(v){this.B=v} }
alert(o.b)
o.b = 123
alert(o.b)
javascript对象属性get/set读取设置器