Var and Javascript variable 'implicit 'Declaration

Source: Internet
Author: User

In JavaScript, var is used to declare a variable, but this syntax is not strictly required. If you modify it many times, you can directly use a variable instead of using var to declare it.
Var x = "XX ";
Y = "xxx ";
And so on. This has a problem. For example, in a certain line of code, I want to use a declared variable x. As a result, the variable is written as y due to incorrect typing or spelling, the result is equivalent to "implicit" declaring a variable y. In actual programming, this error is sometimes hard to be found.

In addition, I learned from my colleagues today about other issues in this "implicit Declaration. When you perform this "implicit" declaration in the current context, the JavaScript engine first checks whether the variable has been declared in the current context. If not, search in the context of the upper level. If it is not found, the variable will be declared in the window!
For example:

Window. y = "hello ";

Function func (){
Y = "OH, NO !!! ";
}

Func ();
Alert (window. y); // # => display "OH, NO !!! "

When any layer in the context contains variables defined by "implicit", the variable at this layer will be modified without generating a new variable on the window. (This bug is also annoying, especially the encapsulated complicated code)
For example:

Var x = "window. x ";
Function (){
Var x = "a's x ";
Var B = function (){
Var c = function (){
// No var!
X = "c's x :";
};
Alert ("before c run, the B. x:" + x );
C ();
Alert ("after c run, the B. x:" + x );
};
Alert ("a. x is:" + x );
B ();
Alert ("after B function runed, the. x is:" + x );
};
Alert ("before a run, window. x:" + x );
A ();
Alert ("after a run, window. x:" + x );

There are the following layers: window, func a, func B, and func are always hierarchical nesting. Window-> a-> B-> c
In Windows and a, both the defined variable x and B do not define the variable. In c, the 'implicit 'declares an x, and the x finally modifies the value of the variable.
Remember to add var before declaring a variable in JavaScript.

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.