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; ... }
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.
source share