<script>
var user = {}
let proxy = new Proxy(user, {
get: function(target, propkey) {
console.log(target, propkey);
return propkey
},
set: function(target, propkey, value) {
if(propkey=="age"){
if(!Number.isInteger(value)){
throw new TypeError("The age is not an integger")
}
if(value>=200){
throw new RangeError("The age seem invalid")
}
}
target[propkey]=value
}
})
proxy.count = 100
proxy.age=10
console.log(proxy.age);
</script>
原文链接:https://juejin.cn/post/7326978201006997541 作者:光影少年