Analyzing scopes in JavaScript

Source: Internet
Author: User

Scopes in 1.javascript


As in other languages, in JavaScript, the scope of a variable also has global and local scope divisions.


2. Global scope


1) All variables in the outermost definition (not defined in the function body) have global scope

2) Direct assignment of variables, automatically default to global scope

<span style= "FONT-SIZE:18PX;" >  <script>        //function out-        of-body var name = "First Name";  global variable        sex = "female";//global variable because the direct assignment Operation        function test () {            //function body Age            = +;  Global variable because there is no add keyword var            var name = "Lucky";        }        alert (name);//return First name    </script></span>

3) The Properties of all window objects have global scope. such as Window.name, etc.

3. Local scope


1) variables defined in the function body with VAR, note that as long as within the function body, regardless of where, because the function is related to nested functions, variables defined with VAR is a local variable, if not added keyword VAR, the system will automatically default to the global variable

<span style= "FONT-SIZE:18PX;" > <script>        Function Test () {            var name = "lucky";//in the function body, local variable            alert (name);        }        Test ();//return lucky    </script></span>




4. Differences between global and local scopes


1) We know that the so-called scope is like wearing clothes, can be nested layers, the outermost is always the global scope, because others can see you wear outside the clothes. Let's look at a small example.


<span style= "FONT-SIZE:18PX;" > <script>                var name = "Name";  Global variable           var people = function () {            var name = "Zhang San";  Local variable            height = 1.8;  Global variable            alert (name);//display Zhang San            alert (window.name);//Display name                   }        people ();        alert (height);//Display 1.8    </script></span>




2) or the above example, if the function people does not have the name of the variable, but the global variables have, what will happen?


<span style= "FONT-SIZE:18PX;" >   <script>                var name = "Name";  Global variable        var people = function () {                       height = 1.8;  Global variable            alert (name);//Display name            alert (window.name);//Display name                   }        people ();        alert (height);//Display 1.8    </script></span>

Explanation: As stated above, scopes are nested in layers. When looking for a variable in JavaScript, it starts with the current scope and then stops if it is found, or vice versa from the outer layer of the function. In this example, we first look for the variable name from the People function, and when it is not found, go to the outer scope and look for the name


3) or the above example, I would change a little bit. or from the scope of the current people function, and people defines the variable, but it is not yet used, so it returns undifined. Note that the variables within the scope, regardless of where the function is declared, JavaScript will include the object in the scope before the function is run.

<span style= "FONT-SIZE:18PX;" > <script>                var name = "Name";  Global variable           var people = function () {                       height = 1.8;  Global variable            alert (name);//Display undifined            alert (window.name);//display name            var name = "Zhang San";  Local variable        }        people ();        alert (height);//Display 1.8    </script></span>


5. Summary

Read the above examples, I do not know if you understand. Simply put, as long as you can distinguish between local variables and global variables OK, and in JavaScript to find variables, is to follow the principle from the inside out, it is so simple.


Analyzing scopes 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.