let student = {};
let age = undefined;
Object.defineProperty(student, 'age', {get: function() {
// 获取该属性时触发
return age;
},
set: function(val) {
// 设置该属性值时触发
age = val;
}
});
student.age = 20;
console.log(student.age); // 20
let student = new Proxy({},
{get: (obj, prop) => {
// 获取该属性时触发
return obj[prop];
},
set: (obj, prop, value) => {
// 设置该属性值时触发
obj[prop] = value;
return true;
}
}
);
student.age = 20;
console.log(student.age); // 20
- 【继续保护 / 更新 500+ 前端面试题 / 笔记】https://github.com/noxussj/In…
- 【大数据可视化图表插件】https://www.npmjs.com/package…
- 【利用 THREE.JS 实现 3D 城市建模(珠海市)】https://3d.noxussj.top/