The problem of "repost" JS variable repeating declaration and ignoring Var declaration and the principle behind it

Source: Internet
Author: User

JS fault tolerance is high, some other languages common small error JS can be generous tolerant, such as to a method to pass over the expected parameters, before declaring the variable (variable declaration promotion solved this problem) and so on, here we will dissect the JS variable repeating declaration and when we ignore Var use a = Secondly, A is a global variable when declaring a variable:

[JavaScript]View PlainCopy 
    1. First paragraph of code
    2. var a = 2;
    3. var a = 3;
    4. alert (a); //3
    5. The second piece of code
    6. <span style="FONT-SIZE:18PX;"  ></span><pre name="code" class="javascript" >a = 2;
    7. alert (a); //2

These two pieces of code in the eyes of JS is completely feasible, JS will silently ignore the second VAR declaration to continue the program execution, and the value of the declaration will overwrite the previously declared value, and the second code JS will ignore the declaration of VAR by default declared as a global variable. This should be clear to everyone, but how does JS run when it encounters a duplicate statement? That's what's behind JS: The engine and his right hand: the compiler and the scope.

During the JS code run:

The engine is responsible for compiling and running the entire code, and the compiler is responsible for lexical analysis, parsing, code generation, and so on, and the scope is responsible for maintaining all identifiers (variables), as we are familiar with.

When we execute the above code, we can simply understand that the new variable is allocated a memory, named a, and assigned a value of 2, but at run time the compiler and the engine will also do two additional operations: to determine whether the variable has been declared:

1. First the compiler parses the code, and from left to right meets Var A, the compiler asks if the scope already has a variable called a, and if it does not exist, the greeting scope declares a new variable a, and if it already exists, ignores Var and continues to compile downward, a = 2 is compiled into executable code for use by the engine.

2. When the engine meets a=2, it will also ask if there is a variable a in the current scope, and if so, assign a value of 2 (because the first step compiler ignores the duplicate declared Var and the scope already has a, so the duplicate declaration will be worth overwriting without error). If not, then follow the scope chain up, if you finally find the variable A is assigned a value of 2, if not found, the greeting scope declares a variable A and assigns a value of 2 (this is why the second piece of code can be executed correctly and a variable is a global variable reason, of course, in strict mode JS will throw an exception directly: A is Not defined).

Although JS is very industrious, can help us solve some small problems, but as a programmer, we had better follow the code to write, in the person's own benefit, why not.

Note: About a = 2 A will be declared as a global variable which involves the LHS query method, if you want to know please move to me another article: JS engine lhs RHS

When writing code, we are always competing with the scope, and how does the engine find what we want along the scope chain? This involves the difference between L and R.

By literal means it is easy to understand that L stands for right on behalf of left R, while LHS and RHS queries can be simply distinguished by the following: When querying the variable on the other side of the = number, the engine uses the LHS and the RHS is used by the engine when querying for the variable at the =. LHS query out is the address of the variable, convenient for the shape of a = 2 assignment operation, because the engine does not need to care about a what is the ghost, according to the requirements of the program Ape 2 to a can be, and RHS query out is the value of the variable stored in order to form a = B assignment operation, the engine also does not need to care Put in the memory of which "lattice", just need to know what is put inside the lattice can be.

Of course, it is not comprehensive to differentiate LHS RHS according to = around, because we can easily miss out some hidden LHS and RHS:

[JavaScript]View PlainCopy 
    1. var c = 3;
    2. function A (b) {
    3. Console.log (B+C);
    4. }
    5. A (2);

In the above section of the code, we can clearly draw C ... Using the Lhs,console.log () in the B, C used RHS, but in the call function A, Console.log also used RHS, the assignment of parameter B is also used LHS, so we'd better by the value, Take the address to determine how the engine uses the query.

For the above code, the engine communicates with the scope:

Engine: global scope, I want to find C, have you seen him?

Global scope: Hey, forget it, the compiler guy just declared it, take it!

Engine: That's great! I'm going to lose 3 to him right now.

Engine: Wait a minute, there's one more thing I want to trouble you with, I want to quote a function, do you know where she is?

Global scope: Is that the guy that was thrown in with C? Here it is, here you are.

Engine: Haha, thank you so much, I just need to put 2 ...

A function scope: Savatika, engine, the weather is good today ah, go out to play together!

Engine: Forget it, I'm busy dying, right, have you ever met a name B?

A function scope: Oh, he's a formal parameter of a function, I happen to have it, take it.

Engine: Dude, I just need to put 2 in B and then ... Hey, little a,console, do you have any?

A function scope: Yes Yes, this is a built-in object, give you

Engine: Haha, you have been so reliable, I look for, hey yo, really have log this function, I have to quickly quote him

Engine: You look at my brain, can you help me find the B, I have to confirm the contents of B

A function scope: rest assured, look! b No change, rest assured

Engine: That's the best, the last step, finish drinking, you there is no C, hand over I ask you a bag of spicy bar!

A function scope: really! I'm looking for, uh ... No, I don't have it here, you have to ask my brother. Global scope

Engine: Global scope, sorry, I came to you again, I do not know if you have C, I take spicy bar with you for

Global scope: Look at you tired of sweating, spicy bar You keep it, C to you, finish the rest now

Engine: You're the best, I'll invite you to dinner in the evening!

After reading the above dialogue, do not know whether you have enough knowledge of LHS RHS, there is one thing to note is that when the global scope is found, if not find the variable information to look for, if the LHS query, the default is to declare a global variable with the same name as the requested variable, and RHS will throw an error, of course, In strict mode, LHS also error, this is the place to pay attention to.

The problem of "repost" JS variable repeating declaration and ignoring Var declaration and the principle behind it

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.