为什么我不能从窗口访问声明的变量?

时间:2017-10-27 14:47:23

标签: javascript

当我运行function从窗口获取所有可用变量时...它只会正确显示非声明变量

宣布变量后:

  1. var
  2. let
  3. const
  4. 代码中断......

    怎么回事? 这可以解决吗?

    抬头
    我不想使用iframe ......

    什么有效

    a = 'apple';
    b = 'banana';
    c = 'cherry';
    d = /dog/gi;
    e = 'elephant';
    f = 'fish';
    g = 'ground';
    h = 'horse';
    
    windowCounter = 0;
    
    function Variables() {
      for (let i in window) {
        if (window.hasOwnProperty(i)) {
          windowCounter++;
          if (windowCounter > 165) {
            console.log(i+' = '+window[i]);
          }
        }
      };
    }
    Variables();



    问题

    let a = 'apple';
    const b = 'banana';
    const c = 'cherry';
    let d = /dog/gi;
    var e = 'elephant';
    f = 'fish';//This is the only one that works properly
    let g = 'ground';
    var h = 'horse';
    
    windowCounter = 0;//And this one…
    
    function Variables() {
      for (let i in window) {
        if (window.hasOwnProperty(i)) {
          windowCounter++;
          if (windowCounter > 165) {
            console.log(i+' = '+window[i]);
          }
        }
      };
    }
    Variables();

0 个答案:

没有答案