Changing a variable out of scope?

Is there a way to change a variable while out of scope? I know, in general, you cannot, but I wonder if there are any tricks or overrides. For example, is there a way to do the following work:

function blah(){ var a = 1 } a = 2; alert(blah()); 

EDIT (for clarification):

In a hypothetical scenario, the variable that is used in the setInterval function, which also goes beyond the scope and into the uneditable previous javascript file, will be changed. This is a pretty stupid scenario, but this is the one I'm going to ask about.

+4
source share
5 answers

Not. No tricks or overrides. You must plan so that both places can see the variable in the same area.

The only trick I can think of regarding an area is to use the window in the browser to navigate to the global object. This can help you move on to a "hidden" variable - one that is in scope, but whose name has been overtaken by a local variable (or another variable closer to the scope chain).

Closures and classes can provide you with some other scope tricks, but none of them can completely redefine scope rules.

+3
source

I donโ€™t understand why you need it, if you need a variable accessible from the outside, just declare it from the outside.

Now, if you ask about it just because you are trying to find out something, itโ€™s good for you.

 var a = 0; function blah() { a = 1; return a; } a = 2; alert(blah()); 
+2
source

You can return a value from a function, of course:

 function blah() { var a=1; return a; } 

But I suppose that is not quite what you had in mind. Since calling a function creates a closure in local variables, it is usually not possible to change values โ€‹โ€‹after creating a closure.

Objects are slightly different because they are reference values.

 function blah1(v) { setInterval(function() { console.log("blah1 "+v); }, 500); } function blah2(v) { setInterval(function() { console.log("blah2 "+va); }, 500); } var a = 1; var b = {a: 1}; blah1(a); blah2(b); setInterval(function() { a++; }, 2000); setInterval(function() { b.a++; }, 2000); 

If you run this in an environment with a console object, you will see that the value reported in blah2 changes after 2 seconds, but blah1 just continues to use the same value for v.

+1
source

Functions can access variables declared outside their scope if they are declared before the function itself:

 var a = 0; function blah() { a = 1; } a = 2; alert(blah()); 

Note that using var a inside a function declared a local variable named a; here we omit the keyword, since otherwise it would hide a as announced in the outer field!

0
source

No, this will never work, but you can use global:

 var a; function blah(){ a = 1 } a = 2; alert(blah()); 

or use closure:

 function bleh() { var a; function blah(){ a = 1 } a = 2; alert(blah()); } 

or you can pass it with a return (which behaves differently, but this is probably what you want to do):

 function blah(a){ a = 1 return a; } a = 2; alert(blah(a)); 
0
source

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


All Articles