How to access function external variable in nested internal function in JS

I am new to JS and doubt the example below. Please see the built-in comments.

function outer() { var x = 5; console.log("outer",x); // Prints 5 console.log("-----------"); function inner() { var x = 6; console.log("inner",x); // Prints 6 console.log("outer",x); // Prints 6. How to print 5 console.log("-----------"); function _inner() { var x = 7; console.log("_inner",x); // Prints 7 console.log("inner",x); // Prints 7. How to print 6 console.log("outer",x); // Prints 7. How to print 5 console.log("-----------"); } _inner(); } inner(); } outer(); 
+6
source share
3 answers

Maybe this will help you. Assign variables to your nested functions, because it is clear which option should be used, but they should somehow differ from each other (name or namespace):

 function outer() { var x = 5; // or via namespace // var out = { x : 5 }; console.log("outer",x); // 5 console.log("-----------"); function inner() { inner.x = 6; console.log("inner",inner.x); // 6 console.log("outer",x); // 5 console.log("-----------"); function _inner() { _inner.x = 7; console.log("_inner",_inner.x); // 7 console.log("inner",inner.x); // 6 console.log("outer",x); // 5 // namespace // console.log("outer",out.x); // 5 console.log("-----------"); } _inner(); } inner(); } outer(); 

In this example, only two of the three variables relate to functions (not external x), because otherwise you could evaluate the external external function external.x and assign it any value:

 function outer(){ outer.x = 5; ... } // assign any value outside outer function outer.x = 34; 

But when a local variable is defined:

 function outer(){ var x = 23; } 

Then there is no way to assign this local variable (x) to any value from an external external function.

+5
source

Javascript did not by default block a scope variable like many programming languages. This means that the variable x, as you stated above, is the same variable.

Variable declarations, wherever they occur, are processed before any code is executed. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

I would suggest not using the same variable name in a nested function. It is hard to read and debug. Also do you need to have a nested function?

 function outer() { var x = 5; console.log("outer",x); // Prints 5 console.log("-----------"); inner(x); } function inner(x1) { var x = 6; console.log("inner",x); // Prints 6 console.log("outer",x1); // Prints 5 console.log("-----------"); _inner(x,x1); } function _inner(x1, x2) { var x = 7; console.log("_inner",x); // Prints 7 console.log("inner",x1); // Prints 6. console.log("outer",x2); // Prints 5. console.log("-----------"); } outer(); 

Or you can use an object declaring it like this

 function outer() { var x = { outer: 5 }; console.log("outer",x,outer); // Prints 5 console.log("-----------"); function inner() { x.inner = 6; console.log("inner",x.outer); // Prints 6 console.log("outer",x.inner); // Prints 6. How to print 5 console.log("-----------"); function _inner() { x._inner = 7; console.log("_inner",x._inner); // Prints 7 console.log("inner",x.inner); // Prints 7. How to print 6 console.log("outer",x.outer); // Prints 7. How to print 5 console.log("-----------"); } _inner(); } inner(); } outer(); 
+3
source

when we reuse the variable name inside the method, then only region-region will work. means the scope will work. Means for these variables, curly braces {.,.} Will determine the scope.

that you can try to define global variables

 var x = 5; var innerx = 5; var _innerx = 5; function outer() { } 
>
0
source

All Articles