1. Local variables are used first and then declared, which does not affect external variables with the same name.
Copy the code code as follows:
var x = 1; // --> external variable x
function fn(){
alert(x); // --> undefined local variable x is used first
var x = 2; // declare and assign later
}
fn();
alert(x); // --> 1<BR>
The first point is that the first sentence in function fn outputs x, and x is defined in the second sentence. This is allowed in JS, and allowed here means that the program can run without syntax errors.
But it is not allowed in other languages such as C and Java. Variables must be declared before use, such as
Copy the code code as follows:
public class Test {
public static void main(String[] args) {
System.out.println(x); //Use first
int x = 10; // Post declaration
}
}
In Java, the compiler will prompt an error and the program cannot run.
The second point is that the local variable x within the function fn will not affect the external variable x. That is, the alert output in fn is not 1, but undefined.
Second, formal parameters have higher priority than function names.
Copy the code code as follows:
function fn(fn){
alert(fn);
}
fn('hello'); // --> "hello"
You can see that the function name and the formal 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.
Third, formal parameters have higher priority than arguments.
Copy the code code as follows:
function fn(arguments){
alert(arguments);
}
fn('hello'); // --> "hello"<BR>
The arguments object can be used directly within the function and is a special identifier provided by the language itself.
Here it happens that the formal parameter is declared with the same name. You can see that the output is "hello" instead of "[object Object]", that is, the formal parameters arguments cover the real arguments provided by the language itself.
4. Formal parameters have higher priority than local variables that are declared but not assigned.
Copy the code code as follows:
function fn(a){
var a;
alert(a);
}
fn('hello'); // --> "hello"
The formal parameter of function fn is a. The first sentence in the function only declares the local variable a, but does not assign a value. From the fact that the output result is "hello" instead of undefined, it can be seen that the formal parameter a has a higher priority than the local variable a that is only declared but not assigned a value.
5. Local variables declared and assigned have higher priority than formal parameters.
Copy the code code as follows:
function fn(a){
var a = 1;
alert(a);
}
fn('hello'); // --> "1"
The formal parameter of function fn is a. The first sentence in the function only declares the local variable a and assigns it a value of 1. From the output result is "1" instead of "hello", we can see that the declared and assigned local variable a has a higher priority than the formal parameter a.
6. When formal parameters are assigned to local variables with the same name
Copy the code code as follows:
function fn(a){
var a = a;
alert(a);
}
fn('hello');
Don't run it yet, guess the result. If you follow point 5: local variables declared and assigned have higher priority than formal parameters. Then a will be undefined. But in fact a is "hello", that is, the right a is the formal parameter a, and the left a is the local variable a.
The two a's here do not interfere with each other, and neither covers the other. This is contradictory to what I just said: assigned local variables have higher priority than formal parameters. But the engine does what we want, because we don't want a to be undefined after var a = a.