This article mainly introduces the priority of identifiers with the same name in JavaScript. For more information, see.
1. Local variables are first used and then declared, without affecting external variables with the same name
The Code is as follows:
Var x = 1; // --> external variable x
Function fn (){
Alert (x); // --> undefined local variable x is used first
Var x = 2; // post declaration and value assignment
}
Fn ();
Alert (x); // --> 1
First, the first sentence of the function fn outputs x, which is defined in the second sentence. This is allowed in JS. The permission here means that the program can run without syntax errors.
However, it is not allowed in other languages such as C and Java. The variables must be declared first and then used, as shown in figure
The Code is as follows:
Public class Test {
Public static void main (String [] args ){
System. out. println (x); // use
Int x = 10; // post Declaration
}
}
In Java, the compiler prompts an error and the program cannot run.
Second, the local variable x in the function fn does not affect the external variable x. That is, the alert output in fn is not 1, but undefined.
2. The parameter priority is higher than the function name
The Code is as follows:
Function fn (fn ){
Alert (fn );
}
Fn ('hello'); // --> "hello"
We can see that both the function name and the form parameter have the same name as fn, and the output is the string "hello", but not the function body (fn. toString () of the function fn ()).
3. The parameter has a higher priority than arguments.
The Code is as follows:
Function fn (arguments ){
Alert (arguments );
}
Fn ('hello'); // --> "hello"
The arguments object can be used directly in the function and is a special identifier provided by the language itself.
The parameter is declared as the same name. The output shows "hello" rather than "[object Object]". The parameter arguments overwrites the true arguments provided by the language.
4. The parameter priority is higher than the local variable that is declared but not assigned a value.
The Code is as follows:
Function fn (){
Var;
Alert ();
}
Fn ('hello'); // --> "hello"
The fn parameter of the function is a. the first sentence in the function only declares the local variable a, but does not assign a value. The output result is "hello" rather than "undefined". It can be seen that the priority of parameter a is higher than that of the declared but unassigned local variable.
5. the declared and assigned local variables have a higher priority than the Parameter
The Code is as follows:
Function fn (){
Var a = 1;
Alert ();
}
Fn ('hello'); // --> "1"
The fn parameter of the function is a. In the first sentence of the function, only the local variable a is declared and the value is 1. The output result is "1" rather than "hello". We can see that the declared and assigned local variable a has a higher priority than the parameter.
6. When a parameter is assigned to a local variable with the same name
The Code is as follows:
Function fn (){
Var a =;
Alert ();
}
Fn ('hello ');
Do not run now. Guess the result. If the value is declared as follows: the priority of the assigned local variable is higher than that of the parameter. Then a will be undefined. But in fact, a is "hello", that is, right a is the form parameter a, and left a is the local variable.
The two a here do not interfere with each other, and no one covers them. This is in conflict with the fact that the priority of the assigned local variable is higher than the parameter. But the engine is really what we want to do this, because we do not want a To be undefined after var a =.