简体   繁体   English

Javascript:筛选已附加到全局范围的变量?

[英]Javascript: Filter variables that have been attached to global scope?

Is it possible to find variables that have been attached to the window scope in a browser? 是否有可能在浏览器中找到附加到窗口范围的变量? Not pre-existing variables, but ones that have been attached by me. 不是预先存在的变量,而是我附加的变量。

Doing console.log(window) gives a long list of variables available, but I only want to see what has been attached by me. 进行console.log(window)会给出一长串可用的变量,但是我只想看看我已经附加了什么。

Well we can pull out any functions that have "[native function]" since those are... native. 好吧,我们可以提取出任何具有"[native function]"功能的函数,因为它们是...本机的。 The following snippet will create an array non_natives of function names that were not pre-existing. 以下代码段将创建一个不存在的函数名数组非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 );
  }
}

If you copy and paste that in your web console, you should be able to view the non_natives array. 如果将其复制并粘贴到Web控制台中,则应该能够查看non_natives数组。

您可以将输出减少为仅变量,请尝试此操作

Object.keys(window)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM