// useProxy.js
const useProxy = () => {
const env_arr = ['document', 'window','navigator', 'localStorage']
const env = {}
env_arr.forEach((value, index, array) => {
env[value] = env[value] ?? {}
env[value] = new Proxy(env[value], {
get(target, p, receiver) {
console.log(`【${value}】 取属性:${p}`)
},
set(target, p, newValue, receiver) {
console.log(`【${value}】 设置属性值:${p} = ${newValue}`)
return true
}
})
})
return env;
}
module.exports = useProxy
使用:
const useProxy = require("./useProxy")
const {window, document, navigator, localStorage} = useProxy()
let innerHeight = window.innerHeight;
window.innerHeight = 123;
// 【window】 取属性:innerHeight
// 【window】 设置属性值:innerHeight = 123
标签:const,useProxy,代理,value,js,window,innerHeight,env,动态
From: https://www.cnblogs.com/fires/p/17785959.html