Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step
https://juejin.cn/column/7244788137410560055
2.02k stars 235 forks source link

代理模式 #488

Open Pcjmy opened 1 year ago

ppjiucai commented 1 year ago

let api = { _apiKey: 'kafakakafaka', name:'kafaka' }; const RESTRICTED = ['_apiKey']; api = new Proxy(api, { get(target, key, receiver) { if(RESTRICTED.indexOf(key) > -1) { throw Error(${key} 不可访问.); } return Reflect.get(target, key, receiver); }, set(target, key, value, receiver) { if(RESTRICTED.indexOf(key) > -1) { throw Error(${key} 不可修改); } return Reflect.set(target, key, value, receiver); } });

// console.log(api._apiKey) console.log(api.name) // api._apiKey = '987654321'