How to get the value of a shadow global variable?

Example:

var test = 'global value'; (function() { var test = 'local value'; // how to get the 'global value' string })(); 

Given the condition that the host environment is unknown, which means that we cannot assume that the global object will be accessible through the name window . In addition, functions are not allowed to accept any arguments!

+6
source share
5 answers

Correction

 var test = 'global value'; (function() { var test2 = 'local value'; console.log(test); })(); 

The real solution is to fix your code so that you don't like your shadow global variables.

Eval works

You can always use global eval, this is the most reliable.

Example

 var test = 'global value'; function runEval(str) { return eval(str); } (function() { var test = 'local value'; console.log(runEval("test")); })(); 

If you don't like defining a global rating, you can use Function to do this indirectly.

Living example

 var test = 'global value'; (function() { var test = 'local value'; console.log(new Function ("return test;") () ); })(); 

Other hacks

The following operations are performed in non-standard mode.

 (function () { var test = "shadowed"; console.log(this !== undefined && this.test); })(); 

And this hack works in broken implementations

 (function() { var test = 'local value'; try { delete test; } catch (e) { } console.log(test); })(); 
+4
source

How to rely on your this as a global object (without explicit reference to window ).

 console.log(this.test); 

jsFiddle .

You can also use the indirect call to eval() ( execScript() here for IE purposes, but you can ignore, as you mentioned, not to assume a browser).

 console.log((window.execScript || eval)('test')); 

jsFiddle .

+1
source

In essence, the big problem, if you want to do this in strict mode, gets the global object. Fortunately, there are a few simple ways: var global = (1,eval)("this"); (which does not work in every browser, since its behavior is undefined in ES3, although it is defined in ES5) or var global = (new Function("return this"))(); (what is he doing).

So put this along with what you need, you can have a function like:

 function getGlobal(name) { "use strict"; var global = (new Function("return this"))(); return global[name]; } 

It would probably be better to avoid the cost of creating a function each time, so it would be better than the following:

 var getGlobal = (function() { "use strict"; var global = (new Function("return this"))(); return function(name) { return global[name]; } })(); 
+1
source

It depends. An “obsessive” way to overcome “not allow any arguments” may be to use call :

 var test = 'global value'; (function() { var test = 'local value'; var globalTest = this.test; }).call(this); 

But if you can consider this global object, unless this is explicitly specified, then this.test will work.

0
source

Another solution is to memoize the global object:

 var test = "global"; function called() { var test = "local"; alert(arguments.callee.window.test); // alerts "global" } called.window = this; called(); 
0
source

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


All Articles