Loading... ## 自动化吐环境 本文主要是实现一个自动化吐环境的方法,对 `Proxy` 和 `Reflect` 不太了解的朋友可以阅读文章[Javascript之Proxy、Reflect](http://t.csdnimg.cn/mlf68),里面对 `Proxy` 和 `Reflect` 做了简单介绍 ### 代理单个对象 ```javascript window = new Proxy(window, { set(target, property, value, receiver) { console.log("设置属性set window", property, typeof value); return Reflect.set(...arguments); }, get(target, property, receiver) { console.log("获取属性get window", property, typeof target[property]); return target[property] } }); document = new Proxy(document, { set(target, property, value, receiver) { console.log("设置属性set document", property, typeof value); return Reflect.set(...arguments); }, get(target, property, receiver) { console.log("获取属性get document", property, typeof target[property]); return target[property] } }); navigator = new Proxy(navigator, { set(target, property, value, receiver) { console.log("设置属性set navigator", property, typeof value); return Reflect.set(...arguments); }, get(target, property, receiver) { console.log("获取属性get navigator", property, typeof target[property]); return target[property] } }); location = new Proxy(location, { set(target, property, value, receiver) { console.log("设置属性set location", property, typeof value); return Reflect.set(...arguments); }, get(target, property, receiver) { console.log("获取属性get location", property, typeof target[property]); return target[property] } }); history = new Proxy(history, { set(target, property, value, receiver) { console.log("设置属性set history", property, typeof value); return Reflect.set(...arguments); }, get(target, property, receiver) { console.log("获取属性get history", property, typeof target[property]); return target[property] } }); screen = new Proxy(screen, { set(target, property, value, receiver) { console.log("设置属性set screen", property, typeof value); return Reflect.set(...arguments); }, get(target, property, receiver) { console.log("获取属性get screen", property, typeof target[property]); return target[property] } }); ``` ### 方法封装 ```javascript function getEnvs(proxyObjs) { for (let i = 0; i < proxyObjs.length; i++) { const handler = `{ get: function(target, property, receiver) { console.log("方法:", "get ", "对象:", "${proxyObjs[i]}", " 属性:", property, " 属性类型:", typeof property, ", 属性值:", target[property], ", 属性值类型:", typeof target[property]); return target[property]; }, set: function(target, property, value, receiver) { console.log("方法:", "set ", "对象:", "${proxyObjs[i]}", " 属性:", property, " 属性类型:", typeof property, ", 属性值:", value, ", 属性值类型:", typeof target[property]); return Reflect.set(...arguments); } }`; eval(`try { ${proxyObjs[i]}; ${proxyObjs[i]} = new Proxy(${proxyObjs[i]}, ${handler}); } catch (e) { ${proxyObjs[i]} = {}; ${proxyObjs[i]} = new Proxy(${proxyObjs[i]}, ${handler}); }`); } } ``` ### 测试代码 ```javascript window = global; proxyObjs = ['window', 'document', 'location', 'navigator', 'history', 'screen'] getEnvs(proxyObjs); console.log(window.test); window.name = "Jim"; console.log(window.name); ``` ### 输出 ```javascript 方法: get 对象: window 属性: test 属性类型: string , 属性值: undefined , 属性值类型: undefined undefined 方法: set 对象: window 属性: name 属性类型: string , 属性值: Jim , 属性值类型: undefined 方法: get 对象: window 属性: name 属性类型: string , 属性值: Jim , 属性值类型: string Jim ``` 最后修改:2024 年 07 月 25 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏