Is there any tool or regexp that might help to get list of all global variables in JavaScript project?
是否有任何工具或正则表达式可能有助于获取JavaScript项目中所有全局变量的列表?
4 个解决方案
#1
8
You can loop through the window object for querying all globally defined data (variables and functions, including predefined ones):
您可以遍历窗口对象以查询所有全局定义的数据(变量和函数,包括预定义的数据):
for (var key in window) {
console.log(key + "=" + window[key]);
}
For analysing the source code, use JSLint.
要分析源代码,请使用JSLint。
#2
2
I quite like the Firebug extension on Firefox.
我非常喜欢Firefox上的Firebug扩展。
Here is the DOM tab enumerating all the properties in the Global (ie. window) object and colouring them by type, these are essentially your global variables.
这是DOM选项卡枚举全局(即窗口)对象中的所有属性并按类型着色,这些基本上是您的全局变量。
#3
1
You can use browser extension that helps with such things like:
您可以使用浏览器扩展来帮助执行以下操作:
-Firebug extension in Firefox
-Firebug在Firefox中的扩展
-Developer Tools window in Chrome
Chrome中的“开发者工具”窗口
-Dragonfly in Opera
-Dragonfly在Opera中
#4
0
I used this regex to show me all the javascript variables in my project code.
我用这个正则表达式向我展示了项目代码中的所有javascript变量。
[a-zA-Z0-9]* = .*;
Then I visually scanned the matches to make sure that all my variables were not global. (i.e. there were 'vars' next to them, so long as they were not parameters passed to a function).
然后我直观地扫描了比赛,以确保我的所有变量都不是全局的。 (即它们旁边有'vars',只要它们不是传递给函数的参数)。
For me, I used Sublime Text 2's awesome multi-file search by allowing regex and disabling the context around the match. I also told the search to only search in the folder containing my own javascript files, so it would not show variables from other frameworks or languages. That made it so that every variable appeared one after the other, and it was a ton easier to see any global variable leaks.
对我来说,我通过允许正则表达式并禁用匹配的上下文来使用Sublime Text 2的令人敬畏的多文件搜索。我还告诉搜索只搜索包含我自己的javascript文件的文件夹,因此它不会显示来自其他框架或语言的变量。这使得每个变量一个接一个地出现,并且更容易看到任何全局变量泄漏。
Hope this helps and wasn't too confusing. I'm a regex newb but this worked for me. I already found 3 global leaks in my own project in about 5 minutes.
希望这有助于并且不会太混乱。我是一个正则表达式的新手,但这对我有用。我已经在大约5分钟内在我自己的项目中发现了3个全局泄漏。
#1
8
You can loop through the window object for querying all globally defined data (variables and functions, including predefined ones):
您可以遍历窗口对象以查询所有全局定义的数据(变量和函数,包括预定义的数据):
for (var key in window) {
console.log(key + "=" + window[key]);
}
For analysing the source code, use JSLint.
要分析源代码,请使用JSLint。
#2
2
I quite like the Firebug extension on Firefox.
我非常喜欢Firefox上的Firebug扩展。
Here is the DOM tab enumerating all the properties in the Global (ie. window) object and colouring them by type, these are essentially your global variables.
这是DOM选项卡枚举全局(即窗口)对象中的所有属性并按类型着色,这些基本上是您的全局变量。
#3
1
You can use browser extension that helps with such things like:
您可以使用浏览器扩展来帮助执行以下操作:
-Firebug extension in Firefox
-Firebug在Firefox中的扩展
-Developer Tools window in Chrome
Chrome中的“开发者工具”窗口
-Dragonfly in Opera
-Dragonfly在Opera中
#4
0
I used this regex to show me all the javascript variables in my project code.
我用这个正则表达式向我展示了项目代码中的所有javascript变量。
[a-zA-Z0-9]* = .*;
Then I visually scanned the matches to make sure that all my variables were not global. (i.e. there were 'vars' next to them, so long as they were not parameters passed to a function).
然后我直观地扫描了比赛,以确保我的所有变量都不是全局的。 (即它们旁边有'vars',只要它们不是传递给函数的参数)。
For me, I used Sublime Text 2's awesome multi-file search by allowing regex and disabling the context around the match. I also told the search to only search in the folder containing my own javascript files, so it would not show variables from other frameworks or languages. That made it so that every variable appeared one after the other, and it was a ton easier to see any global variable leaks.
对我来说,我通过允许正则表达式并禁用匹配的上下文来使用Sublime Text 2的令人敬畏的多文件搜索。我还告诉搜索只搜索包含我自己的javascript文件的文件夹,因此它不会显示来自其他框架或语言的变量。这使得每个变量一个接一个地出现,并且更容易看到任何全局变量泄漏。
Hope this helps and wasn't too confusing. I'm a regex newb but this worked for me. I already found 3 global leaks in my own project in about 5 minutes.
希望这有助于并且不会太混乱。我是一个正则表达式的新手,但这对我有用。我已经在大约5分钟内在我自己的项目中发现了3个全局泄漏。