JavaScript variables: Global or local? Pay attention to this!

Source: Internet
Author: User
Tags definition end variables range variable window

Doing the project is a learning process!

As a learning C3, from C programmers to the front end, many of the rules in JavaScript is no way or say 1:30 will not understand.

I met one today, which is roughly the code.


var A; Global variables
function Fun ()
{
alert (a); A is assigned in other places and operates here, such as assigning a value of 0-------------1
if (a==0)
{
alert (a); ----------------2
A = 10;
}
var a = 5; Redefining variables
alert (a); -----------------3
}
function fun2 ()
{
Alert (a)//------------------4
}
The assignment is performed first, then the fun () is executed, and the fun2 () is executed;



The results of the 2nd operation how can not enter, so hit the 1th direct a value is: undefined!, I C, what is this situation?

May be the theory of C, in the mind of too deep, think for a long time also want to do not understand why, had to check on the Internet, fortunately I was not a person encountered such a problem, online check three articles.

JavaScript variable Range

(1) Global variables are scoped globally, that is, the global variable function exists everywhere in the entire JavaScript program. Defined in the "script" block, outside of the "function" function.
(2) The scope of the local variable is localized, defined within the function or function parameter, and the scope is from the beginning of the function to the end (note here)
(3) Within a function, a local variable has a higher precedence than a global variable of the same name, and if there are local variables (including parameters) that are the same as the names of global variables, then the global variable will no longer work.

When I saw this, I seemed to understand a little bit.

That is, my definition of callout 3 is no different from the first line of fun definition, because as long as the function is defined internally, its scope is from start to end (the definition is not assigned, so undefined).

By the way, I read some deeper knowledge.

The range of JavaScript language blocks is different from others, such as C, which are divided by function, called method blocks (which are divided according to the following {}).

In other words, for,while, and so on internally defined variables, although {}, but out of {}, or valid.

Example: Q,x,y,z are defined in different positions of functions, but their scope is the same, are equivalent to the beginning of the definition of the function, good magic, or TMD difficult to understand Ah, is there?


function Test (q)
{
In the entire function, X has meaning
var x=0;
if (typeof (q) = = "Object")
{
Y not only has meaning in the if block, it's meaningful throughout the function
var y=0;
for (Var z=0;z<5;z++)
{
Z not only has meaning in the for loop, it's meaningful throughout the function
document.write (z);
}
document.write (z); Z still has meaning, output 5
}
document.write (y); Y still make sense.
}

Question: What if alert (x) is here?



According to the above three points it should be easy to see: If the outside of the function alert (x), there will be fatal error, the script stopped! Since x is not defined outside of the function.

So how do we get the value of a global variable when the local variable is the same as the global variable?

Haha, the simplest of course is to avoid the global variable and local variable encounter!

In addition, use window. Global variable Name

Example:




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.