📜  javascript 列出所有声明的变量 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:04.252000             🧑  作者: Mango

代码示例1
function storeVars(target) {
  return new Proxy(target, {
    has(target, prop) { return true; },
    get(target, prop) { return (prop in target ? target : window)[prop]; }
  });
}
var vars = {}; // Outer variable, not stored.
with(storeVars(vars)) {
  var a = 1;   // Stored in vars
  var b = 2;   // Stored in vars
  (function() {
    var c = 3; // Inner variable, not stored.
  })();
}
console.log(vars);
 Run code snippetHide results