How to determine the creation of new global variables?

I want to monitor the creation of new global variables in Javascript so that an event is fired at any time the global variable is created.

I heard about the watch () function, but it is only for viewing certain variable names. I want to access them.

+4
source share
4 answers

I do not know how to make this work "on demand" as soon as var is created, but I can offer a survey. In the browser window, all global become members of the global "window" object. (Because technically the "window" is a "global object"). So you can do something like the following:

1) list all the properties in the window

window.proplist = window.proplist || {}; for (propname in window) { if (propname !== "proplist") { window.proplist[propname] = true; } } 

2) Set a timer for periodic "polling" of the window for new properties

 setInterval(onTimer, 1000); 

3) Wake up on a timer callback and look for new details

 function onTimer() { if (!window.proplist) { return; } for (propname in window) { if (!(window.proplist[propname])) { window.proplist[propname] = true; onGlobalVarCreated(propname); } } } 
+3
source

If you already know which names pollute your global namespace (see Intercepting the definition of a global variable in javascript ), you can use this trick to find out when it actually happens

 window.__defineSetter__('someGlobalVar', function() { debugger; }); 

Make sure your developer tools are open when you run this. Obviously, it only works if your browser supports __defineSetter__ , but this is true for modern browsers. Also, be sure to remove the debug code after you are done.

Found it here .

+2
source

Afaik, .watch() is just SpiderMonkey (Firefox).

I played with the polling function, I finally came up with this:

 var mywatch = (function() { var last = { count: 0, elems: {} }; return function _REP(cb) { var curr = { count: 0, elems: {} }, diff = {}; for(var prop in window) { if( window.hasOwnProperty(prop) ) { curr.elems[prop] = window[prop]; curr.count++; } } if( curr.count > last.count ) { for(var comp in curr.elems) { if( !(comp in last.elems) ) { diff[comp] = curr.elems[comp]; } } last.count = curr.count; last.elems = curr.elems; if(typeof cb === 'function') cb.apply(null, [diff]); } setTimeout(function() { _REP(cb); }, 400); }; }()); 

And then use it like:

 mywatch(function(diff) { console.log('NEW GLOBAL(s): ', diff); }); 

Keep in mind that only new global variables are used for this. But you can easily expand this for the case last.count > curr.count . This indicates that global variables have been deleted.

0
source

You cannot fire an event when some script does var v = 10 , but as selbie said, you can poll the window object ... I wanted to offer the same thing, but it beat me. Here is my other example ... you count how many window objects are and execute the GlobalVarCreated () function:

 var number_of_globals = 0; //last known globals count var interval = window.setInterval(function(){ var new_globals_count = 0; //we count again for(var i in window) new_globals_count++; //actual counting if(number_of_globals == 0) number_of_globals = new_globals_count; //first time we initialize old value else{ var number_of_new_globals = new_globals_count - number_of_globals; //new - old if(number_of_new_globals > 0){ //if the number is higher then 0 then we have some vars number_of_globals = new_globals_count; for(var i = 0; i<number_of_new_globals; i++) GlobalVarCreated(); //if we have 2 new vars we call handler 2 times... } } },300); //each 300ms check is run //Other functions function GlobalVarCreated(){} function StopInterval(){window.clearInterval(interval);} 

You can download this code in the Chrome or FF console only: function GlobalVarCreated(){console.log("NEW VAR CREATED");} and test it:

var a = 10

b = 10

The NEW VAR CREATED displayed 2 times.

0
source

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


All Articles