function get_environment(proxy_array) {
for (var i = 0; i < proxy_array.length; i++) {
// 正确构建Proxy handler对象
const handler = {
get: function(target, property, receiver) {
console.log("方法: ", "get ", "对象: ",
proxy_array[i],
"属性: ", property,
"属性类型: ", typeof property,
"属性值类型: ", typeof target[property]);
return target[property];
},
set: function(target, property, value, receiver) {
console.log("方法: ", "set ", "对象: ",
proxy_array[i],
"属性: ", property,
"属性类型: ", typeof property,
"属性值类型: ", typeof target[property]);
return Reflect.set(...arguments);
}
};
try {
// 先检查对象是否存在
eval(`if (typeof ${proxy_array[i]} === 'undefined') { ${proxy_array[i]} = {}; }`);
// 应用Proxy
eval(`${proxy_array[i]} = new Proxy(${proxy_array[i]}, handler);`);
} catch (e) {
console.error(`无法为 ${proxy_array[i]} 设置代理:`, e);
eval(`${proxy_array[i]} = new Proxy({}, handler);`);
}
}
}
// 使用示例
const proxy_array = ['window', 'document', 'location', 'navigator', 'history', 'screen'];
get_environment(proxy_array);