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

Source: Internet
Author: User

Doing the project is a learning process!

As a learning C3 years, from the C programmer to the front-end people, JavaScript in many of the rules are no way or to say 1:30 will not understand.

Today I met one, which is roughly the code.

varA//Global VariablesfunctionFun () {alert (a); //A is assigned elsewhere, and operates here, for example, with a value of 0-------------1    if(a==0) {alert (a); ----------------2 A= 10; }            varA = 5;//Redefining Variablesalert (a); -----------------3}functionfun2 () {alert (a)//------------------4}//the assignment is performed first, then the Fun () is performed, and the Fun2 () is executed.

Results 2nd operation How all can not enter, so played 1th place 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 not understand why, had to check on the Internet, fortunately not I was a person encountered such a problem, in the online search three articles.

javascript variable range ( 1 Global variables are globally scoped, that is , throughout the JavaScript program, global variables exist everywhere. Defined in the "script" block, outside of "function". (2) The scope of a local variable is local, defined within a function or function parameter, and is scoped from the beginning of the function to the end ( note here ) (3) inside the function, Local variables have higher precedence than global variables with the same name, and if there are local variables (including parameters) with the same name as the global variable, the global variable will no longer work.

When I saw this, I seemed a little bit clear.

That is to say I am in the definition of 3 , with the first line in the fun definition is no different, because as long as the function is defined internally, its scope is from the beginning to the end ( when the definition is not assigned, so undefined).

By the way, I saw some deep knowledge.

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

That is, for,while, such as internally defined variables, although there is {}, but out of {}, still valid.

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

functionTest (q) {//in the whole function, X has meaning.       varX=0; if(typeof(q) = = "Object")      {           //y is meaningful not only in the If block, but also in the entire function          varY=0;  for(varz=0;z<5;z++)         {                        //Z has meaning not only in the for Loop, but also in the entire functiondocument.write (z);             } document.write (z); //Z still has meaning, output 5} document.write (y); //y still has meaning .}

Question: What happens if alert (x) is here?

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

So, how do you get the value of a global variable when the local variable has the same name as the global variable?

Haha, the simplest of course is to avoid global variables and local variables meet!

In addition, use window. Global variable Name

Example:

<script>      var a =0;         // Global A definition
function Test () {     
        alert (WINDOW.A);   // A is 1, and a here takes a global variable  .        var a=2;     // local variable A definition          alert (a);     // A is 2, and a here is a local variable!       }   
test (); alert (a); // Global A, of course, can also be used WINDOW.A</script>

At this point, the problem is also understood!

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

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.