Is there a method to access and print the value of a closure variable, temp, from a function defined outside the closure but referenced within it without passing temp as an argument to the function?
var funcA, funcB;
funcA = function () {
console.log(temp);
}
funcB = function () {var temp, funcC;
temp = 1;
funcC = funcA;
funcC();
}
funcB(); // temp is undefined.
This solution works only when funcA is defined inside funcB:
funcB = function () {var temp, funcA, funcC;
temp = 1;
funcA = function () {
console.log(temp);
}
funcC = funcA;
funcC();
}
funcB(); // 1
I am attempting to simplify some complicated code by moving certain function definitions out of outer functions. Is there a way to define funcA externally to funcB but still reference the temp variable without needing to pass parameters?
Although Javascript is said to have lexical scoping rather than dynamic runtime scoping, can referencing a function (such as funcA via funcC) in funcB provide access to scoped variables while meeting the requirements of lexical scope?