【问题标题】:Javascript: Filter variables that have been attached to global scope?Javascript:过滤已附加到全局范围的变量?
【发布时间】:2014-11-23 03:18:28
【问题描述】:

是否可以在浏览器中找到已附加到窗口范围的变量?不是预先存在的变量,而是我附加的变量。

执行 console.log(window) 会给出一长串可用的变量,但我只想看看我附加了什么。

【问题讨论】:

    标签: javascript browser scope window global


    【解决方案1】:

    您可以将输出减少到仅变量,试试这个

    Object.keys(window)
    

    【讨论】:

      【解决方案2】:

      好吧,我们可以提取任何具有"[native function]" 的函数,因为它们是......原生的。以下 sn-p 将创建一个数组non_natives,其中包含不预先存在的函数名称。

      var non_natives = [];
      
      for ( fin in window ) {
        if ( typeof(window[fin]) === "function" && window[ fin ].toString().indexOf("[native code]") === -1 ) {
          non_natives.push( fin );
        }
      }
      

      如果您将其复制并粘贴到 Web 控制台中,您应该能够查看 non_natives 数组。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-30
        • 2012-07-17
        • 2013-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-05
        相关资源
        最近更新 更多