- 微信扫码关注公众号 :前端前端大前端,追求更精致的阅读体验 ,一起来学习啊
- 关注后发送关键资料,免费获取一整套前端系统学习资料和老男孩python系列课程
描述
代理模式更像是一个中转的过程,无法一步完成。就像网购,平台就是代理,买家的钱和卖家的货都由平台代理完成每一笔交易。在家访问公司内网,也需要设置代理。设置代理前后并不会改变原始地址,只是起到一个中间人的作用。
code
let boss = {
name: "tom",
age: 35,
phone: "老板电话:153xxxx6489"
}
let secretary = new Proxy(boss, {
get: (target, key) => {
if (key === 'phone') {
return '秘书电话:136xxxx1543'
}
return target[key];
},
set: (target, key, value) => {
if(key==='orderPrice'){
if(value<200000){
console.error('交易金额未满足要求');
return true;
}else{
target[key]=value;
return true;
}
}
}
})
console.log(secretary.name)//老板姓名
console.log(secretary.age)//老板年龄
console.log(secretary.phone)//秘书电话
//客户联系秘书,要和老板谈生意
//secretary.orderPrice=10000;
//console.log(secretary.orderPrice)//报错
secretary.orderPrice=300000;
console.log(secretary.orderPrice)//正常运行