An analysis of the identifier priority _javascript technique in JavaScript

Source: Internet
Author: User
Tags object object

First, local variables are declared after use 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 x first use
var x = 2; After declaring and assigning value
}
FN ();
alert (x); --> 1<br>

1th, the first sentence output x,x in the function FN is defined in the second sentence. This is allowed in JS, where the permission is that there is no syntax error program can run.

But it is not allowed in other languages such as C,java. Variables must be declared before use, such as

Copy Code code as follows:

public class Test {
public static void Main (string[] args) {
SYSTEM.OUT.PRINTLN (x); First Use
int x = 10; Post Declaration
}
}

The compiler in Java prompts for an error and the program cannot run.

2nd, the local variable x within the function FN does not affect the external variable x. That is, the FN alert output is not 1, but undefined.

Second, formal parameter precedence is higher than function name

Copy Code code as follows:

function fn (FN) {
Alert (FN);
}
fn (' hello '); --> "Hello"

You can see that the name of the function and the name of the parameter are FN, and that the string "Hello" is not the function body (fn.tostring ()) of the function fn.

Third, formal parameter precedence is higher than arguments

Copy Code code as follows:

function fn (arguments) {
alert (arguments);
}
fn (' hello '); --> "Hello" <BR>

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

This just declares the formal parameter to be the same as its name. The output can be seen as "hello" instead of "[Object Object]", that is, the formal parameter arguments covers the real arguments provided by the language itself.

Four, parameter precedence is higher than only declared but not assigned value of local variables

Copy Code code as follows:

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

The function fn parameter is a, the first sentence of the function declares only the local variable a, but is not assigned a value. From the output is "hello" instead of undefined, you can see that the parameter a precedence is higher than only the declared but not assigned value of local variable a.

Five, the declared and assigned local variable has precedence over the formal parameter

Copy Code code as follows:

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

The function fn parameter is a, the first sentence in the function declares only the local variable a, and the assignment is 1. From the output is "1" rather than "hello" you can see that the declared and assignment of the local variable a priority higher than formal parameter a.

When a parameter is assigned to a local variable with the same name

Copy Code code as follows:

function fn (a) {
var a = A;
alert (a);
}
fn (' hello ');

Temporarily do not run, guess the result. If you follow the 5th: declared and assigned local variables have precedence over formal parameters. Then a will be undefined. But actually a is "hello", that is, the right A is the formal parameter a, and the left a is the local variable A.



The two aces here are not interfering with each other, and no one is covering anyone. This is in contradiction with the argument that the local variable precedence of the assigned value is higher than the formal parameter. But the engine does what we want because we don't want var a = A after a is undefined.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.