On the scope of JavaScript

Source: Internet
Author: User

First, look at what the following code will output.

Example one:

<script type= "Text/javascript" >
var age = "All";
var name= "Wusuopubupt";
function echo () {
	alert (name);
	var name = "CZXTTKL";
	alert (name);
	alert (age);
}
Echo (name);
</script>

At first, I think the result is:

Wusuopubupt
CZXTTKL
23

But the actual output is:

undifined
CZXTTKL
23

Why, then?

"The JavaScript Authority Guide" says:

1. Within a function body, a local variable has a higher precedence than a global variable of the same name. If the name of a local variable or function is declared the same as the name of a global variable, then the global variable is effectively hidden.

The functions in 2.JavaScript run in their defined scopes, rather than in the scopes in which they are executed.

3. Foo is a global variable when the function body declares the variable foo without the var identifier.


Look at one more example:

Example two:

<script type= "Text/javascript" >
var age = "All";
var name= "Wusuopubupt";
function echo () {
	alert (name);
	Name = "CZXTTKL"; Look, there's no var!
	here. alert (name); 
	alert (age);
}
Echo (name);
</script>

Output what.

Wusuopubupt
CZXTTKL
22
This is the result we want.


Why, then?

--Because JavaScript script execution is precompiled first. (Reference: http://www.laruence.com/2009/05/28/863.html)


Principle and Explanation:

For example 1:

Because the local variable var name= "CZXTTKL" is redefined in the body of the function, when the function executes to the first alert (name), the outside global variable is overwritten by the local variable, The local variable name is not yet defined (because it is defined under the first alert (name).) , while JavaScript's precompilation tells the compiler that a variable of name is present, note that the name here has a value, but its value is undefined (undefined), which is a good explanation for the origin of the undefined in example one.

To illustrate the difference between undifined and error, look at the following example:

<script type= "Text/javascript" >
var name= "Wusuopubupt";
function echo () {
	alert (age);
}
Echo ();
</script>

Without any output, view the chrome log:

You can see that the value of age was not undefined (undefined), but it was an error: The age was not defined, which does not exist at all. )。 It is now possible to prove that the first name in example one exists, except that the value of name is undifined.


For example 2:

Simply because the Var identifier is not used when declaring the variable name = "CZXTTKL" Inside the function body, name is a global variable, so the first alert (name) outputs a global variable (var name= for life outside the function body). Wusuopubupt ").


Reference:

1. "Javascript:the Definitive Guide"

2. Laruence Daniel: Http://www.laruence.com/2009/05/28/863.html

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.