Deep understanding of the scope of JavaScript

Source: Internet
Author: User

To put it simply, scopes are the accessible scope of variables and functions, that is, scope controls the visibility and life cycle of variables and functions. In JavaScript, the scope of a variable is divided into both global and local.

Having a global scope is accessible anywhere in the code, called a global variable , and the following three scenarios can have

Global scope

Variables defined outside the outermost function and the outermost function:

var name= "Brizer"; function dosomething () {    var realname= "LF";    function Innersay () {        alert (realname);    }    Innersay ();} document.write (name); Brizerdocument.write (Realname); Script error dosomething (); Lfinnersay ()//Script error

Variables that are not directly assigned are defined:

/* Global scope 2:*/function dosomething () {    var name= "Brizer";    Realname= "LF";    document.write (name);} DoSomething (); Brizerdocument.write (Realname); Lf

All Window objects:

In general, the built-in properties of all window objects have global scope, such as window.location.

Local scope

Local scopes are accessible only within a fixed code fragment.

The following code:

function dosomething () {    var name= "LF";    function Innersay () {        alert (name);    }    Innersay ();} alert (name); Script error Innersay (); Script error

function scope

In this case, the function scope is presented separately, not because it is a lateral relationship with local and global, but because it is more special . The functions in JavaScript run in the scope they are defined , not in the scope in which they are executed. This is a sentence in the JavaScript authoritative guide, quite classic.

To give an example,


Functions run in the scope they are defined, not the scope in which they are executed  var name = ' LF ';     function echo () {          document.write (name);     }      function env () {          var name = ' Brizer ';          Echo ();     }      Env ();

The last result of LF rather than brizer. When a function is defined, its scope is determined.

Scope chain

Here we talk about the scope chain, in JavaScript, the function is also an object, and in fact, everything in JavaScript is an object. Inside the function is an internal property that is accessible only to the JavaScript engine, which contains a collection of objects in the scope at which the function was created, called the scope chain .

For example, the following code:

function Add (num1,num2) {    var sum = num1 + num2;    return sum;}

When the function is created, it fills a global object in its scope chain, which contains all global variables, such as:

When the function is executed, an active object is created that contains all the local variables, named arguments, and this for the function, which is then pushed into the front end of the scope chain, and the object is destroyed as soon as the function finishes executing. Such as:


As you can see, global variables are pushed to the last end of the scope chain by the active object, which is why global variable access is slow !


With

In general, scope chains are only affected by the with and catch statements. When the creation is used with, the function creates a new active object and pushes it to the front end, which is the object with. This means that all local variables are in the second scope chain object, which is why you should avoid using with .

function Initui () {with    (document) {        var bd=body,            links=getelementsbytagname ("a"),            i=0,            len= Links.length;        while (I < len) {            update (links[i++]);        }        getElementById ("Btninit"). Onclick=function () {            dosomething ();}        ;}    }




Deep understanding of the scope of 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.