Usage of variables in Javascript

Source: Internet
Author: User
Tags define function

All variables in JavaScript are of loose type. Different from other object-oriented speech, variable declarations are of strong type. Therefore, JavaScript variable declarations do not include types. Declare a variable by using the VaR keyword or directly writing the variable name, such:
VaR v = 1;
V = 1;

At this time, someone may ask, what is the difference between the two declarations, and why there are these two different declaration methods, which involves the scope of variables in JavaScript. In JavaScript, the scope of a variable includes global and function-level.

Global variables can be declared in the external body of the function. No matter which method described above is used, the variables declared in the external body of the function are global variables. For example:CopyCodeThe Code is as follows: <SCRIPT type = "text/JavaScript" Language = "JavaScript">
VaR v = 1;

Function Foo ()
{
Alert (v );
}

W = 2;
Function bar ()
{
Alert (w );
}

Foo ();
</SCRIPT>

Running result: 1 2

In addition, if the VaR keyword is not used, the declared variable is also a global variable. For example:Copy codeThe Code is as follows: <SCRIPT type = "text/JavaScript" Language = "JavaScript">
Function Foo ()
{
V = 1;
}

Foo ();
Alert (v );
</SCRIPT>

Running result: 1
However, in this case, to use a variable, you must first call the function that declares the variable to initialize the variable, such as Foo (). Otherwise, the error "variable v undefined" will occur.

Global variables exist as the properties of the window object because they can be accessed through window. $ ($ indicates the variable name. Of course, you can also directly access it using the variable name. The following describes why these two access methods are available.

Variables declared through the VaR keyword in a function are function-level variables, and their scope is limited only within the function. For example:Copy codeThe Code is as follows: <SCRIPT type = "text/JavaScript" Language = "JavaScript">
Function Foo ()
{
VaR v = 1;
Alert (v );
}

Alert (v );
</SCRIPT>

Running result: 1 variable "v" is not defined

Through the above analysis, we can find that the keyword VaR is mainly used to define function-level variables.

A careful friend may ask, if the same variable is defined inside and outside the function, what kind of result will it be? For example:Copy codeThe Code is as follows: <SCRIPT type = "text/JavaScript" Language = "JavaScript">
VaR v = 1;
Function Foo ()
{
Alert (v );
VaR v = 2;
}

Foo ();
</SCRIPT>

Running result: Undefined
!!!!! Some people may be depressed. V clearly defines the function Foo () in vitro. Why is it undefined? This involves parsing JavaScript. Based on experience, the parsing process of JavaScript variables in the function body is:
Search for all var keywords, place the variable declaration at the beginning of the function body, and keep the value assignment and usage unchanged. In this way, the above Javascript is actually equivalent:Copy codeThe Code is as follows: <SCRIPT type = "text/JavaScript" Language = "JavaScript">
VaR v = 1;
Function Foo ()
{
VaR V;
Alert (v );
V = 2;
}

Foo ();
</SCRIPT>

According to this analysis, the above results are obvious, because the priority of the variables in the function is higher than that of the global variables (most ofProgramming LanguageThe variable V in the function overwrites the global variable V, but the result is undefined because it is declared but not assigned a value when the variable V in the function is used.

If you still need to use the defined global variable V in the method body, the window object is very useful at this time and can be accessed through window. v. For example:Copy codeThe Code is as follows: <SCRIPT type = "text/JavaScript" Language = "JavaScript">
VaR v = 1;
Function Foo ()
{
Alert (window. V );
Alert (v );
VaR v = 2;
}

Foo ();
</SCRIPT>

Running result: 2 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.