This article mainly introduces the differences between javascript nested functions and calling external functions in functions, we all know that the local variables defined in the function are always defined in the declared function Body and Its nested functions, in addition, an object always points to a global object in the function scope chain, so that the function can access global variables.
Var ga = 'global'; var func = function () {var la = 'local'; return function () {return function () {alert (la); alert (ga) ;}}} a = func (); a (); // The local and global
So when function A is called by function B in the body of the function, can A access the local variables defined in function B? The answer is no. Modify the above example as follows:
var ga = 'global';function repeat() { alert(la); }var func = function() { var la = 'local'; alert(1); repeat(); alert(2);};func();
The above running result only pops up 1. When calling repeat, the program is interrupted because the undefined variable js interpreter is accessed.
The reason is that the function saves a scope chain during definition. The repeat function is defined externally and there is no local variable named la in its scope, if you continue to search for la in the global scope, an error is reported.
Therefore, nested functions and nested calls to external functions are quite different.
Answer a question yesterday. I hope this article will help friends who are equally confused.