Intercepting global variable definition in javascript

I am trying to remove some javascript code, and one of the steps removes all useless (or simple incorrect) global variables that slip from errors like:

for (prop in obj) { ... 

instead

 for (var prop in obj) { ... 

JSLint helps to understand this muck a little, but it is not 100% reliable when muck happens during the work. I already tried to add some control code that regularly checks the global area to the console if some new variable is found , and this helped a little, but when it tells me that a new global variable named "I" was found .. ... well, this is a mess figuring out where it happened in thousands of lines of code.

So here we are: is there a better way / tool / script / everything to find small pests? My dream is something like a Firebug plugin that stops execution whenever a new global variable is created ...

Thanks!

+2
source share
4 answers

You may find this bookmarklet useful.

Also check this answer: How to determine if new global variables are created?

+3
source

I wonder if you can set a timeout to create a list of all global variables, and then compare this to the last timeout expiration. I found this on Stack Overflow, and maybe you could use this code along with setTimeout () to get what you want.

Blockquote Yes and no. No in almost all situations. Yes, but only in a limited way if you want to check global reach. Take the following example: var a = 1, b = 2, c = 3;

 for ( var i in window ) { console.log(i, typeof window[i], window[i]); } 

Stack Overflow Link: Retrieving All Variables in an Area

0
source

Ok, I wrote this a long time ago, so the code sucks, but it does the job: https://gist.github.com/1132193 insert the firebug console or enable it as a script.

0
source

You say you are trying to remove some kind of code. In this case, use an IDE, such as NetBeans PHP (free) or JetBrains WebStorm ($ 30). They both color global variables and do many other useful things;) If your polling script continues to detect the creation of global variables - track the violation functions and make them suffer;) In the end, the code will become clean.

0
source

Source: https://habr.com/ru/post/1340923/


All Articles