Home > Web Front-end > JS Tutorial > A brief analysis of the priority of identifiers with the same name in JavaScript_javascript skills

A brief analysis of the priority of identifiers with the same name in JavaScript_javascript skills

WBOY
Release: 2016-05-16 17:10:31
Original
1310 people have browsed it

1. Local variables are used first and then declared, and do not affect external variables with the same name

Copy code Code As follows:

var x = 1; // --> External variable x
function fn(){
alert(x); // --> undefined local variable Use x first
var x = 2; // Then declare and assign
}
fn();
alert(x); // --> 1


First point, 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 code The code is as follows:

public class Test {
public static void main(String[] args) {
System.out.println(x); // Use
first int x = 10; // Then declare
}
}

The compiler in Java will prompt an error and the program cannot run.

Second point, 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, the formal parameters have higher priority than the function name

Copy the code The code is 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 it is not the function body (fn.toString()) of the function fn.

Three, formal parameters have higher priority than arguments

Copy code The code is as follows:

function fn(arguments){
alert(arguments);
}
fn('hello'); // --> "hello"

The

arguments object can be used directly within the function and is a special identifier provided by the language itself.

This is where 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.

Fourth, formal parameters have higher priority than local variables that are declared but not assigned

Copy code The code is as follows:

function fn(a){
var a;
alert(a);
}
fn('hello'); // - -> "hello"

The function fn formal parameter 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, we can see 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 code Code As follows:

function fn(a){
var a = 1;
alert(a);
}
fn('hello'); // - -> "1"

The function fn formal parameter is a. The first sentence in the function only declares the local variable a and assigns it a value of 1. From the fact that 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 the formal parameter is assigned to a local variable with the same name

Copy the code The code is 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.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template