Home > front end >  Consult!
Consult!

Time:11-27



Why these two code is the last one to the console. The log to print out the results of different?

CodePudding user response:

Function fn1 (a) and that the parameters of a fn1 function within the local variable a,
When you call fn1 () no arguments to a value, a value is the default value is undefined,
Within fn1 function to access a, access is a function of the local variable a,
Within the fn1 function for a new assignment, change is also a function of the local variable a, a global variable outside the function is not affected,

If the function fn1 () is not set parameter a, there is no local variable in a function. A,
In a, fn1 function access is the global variable outside the function of a,
Within the fn1 function for a new assignment, change is a global variable outside the function of a,

CodePudding user response:

Upstairs said yes, on the whole is:
Function within the operating variables, first of all to judge to have local variables with the same name, have the operation for local variables, as global variables, conversely

CodePudding user response:

Is simply the first local affirmed the variables (parameter is declared variables), modify and print are all local variables. The second piece of code had not declared that the local variables, so to find global variables

CodePudding user response:

And the first fn1 should be covered by the following fn1, actually perform 1,2,2,1,2,2 prints,