ES6基础

This commit is contained in:
罗祥
2019-12-16 16:31:44 +08:00
parent 0b506230fe
commit 085632c43f
5 changed files with 208 additions and 6 deletions

View File

@ -0,0 +1,25 @@
let person = {
name: "heibai",
idCard: null
};
let proxy = new Proxy(person, {
set(target, propertyKey, value, receiver) {
if (propertyKey === "idCard" && value.length !== 18) {
throw new Error("输入的身份证长度必须为18位");
}
// 调用反射进行修改
return Reflect.set(target, propertyKey, value, receiver);
},
get(target, propertyKey, receiver) {
if (propertyKey === "idCard") {
throw new Error("身份证是私密信息,无权读取");
}
// 调用反射获取值
return Reflect.get(target, propertyKey, receiver);
}
});
proxy.name = "ying";
console.log(proxy.name);
proxy.idCard = "123456789123456789";
console.log(proxy.idCard);