JavaScript scope principle (i)--scope chain

Source: Internet
Author: User
Tags variable scope

I. Description of scope

There is an incisive description of scopes in the JavaScript authoritative guide: "The functions in JavaScript run in their defined scopes, not in the scopes they are executed in." ”

In JavaScript, the concept of scopes is almost the same as in other languages, and each time a function is called, it enters a scope within a function, and when returned from the function, returns the scope before the call.

Verify the words in the authoritative guide:

<id= "Scope1"  style= "color:red"></p >
function Echo (p, HTML) {            + = html + ' <br/>'        ;        } // 1.        var position = ' out ';         var pscope1 = document.getElementById (' scope1 ');         function scope1 () {            echo (pscope1, position);        }         function Set1 () {            var position = ' in ';            Scope1 ();        }        Set1 ();

1, variable scope1 positioning to a P tag, for printing information display

2,Echo is a custom function for printing information, parameter one is the P tag, one is to print the information content

3, the final print out is "out", because the printing operation in the scope1 function, there is no variable position in thescope1 , can only go to the outer layer of the scope of the lookup, the scope of the relationship as shown:

4 . The scope chain of the call Set1 looks like this:

Second, the implementation of the scope

The implementation of the scope is not a "stack" but rather a list of uses, as described in ECMA262:

    1. The scope of any execution context time is implemented by the scope chain (scope chain)

    2. When a function is defined, the scope chain that defines the moment is linked to the [[scope]] property of the Function object

    3. When a function object is called, an active object (that is, an object) is created, and then, for each function's formal parameter, it is named as the active object's named property, then the active object is the front-end of the scope chain (scope chain) at this time, and this function object's [[ Scope]] added to scope chain

Take a look at two examples and practice practiced hand:

function function1 () {            var x = ' in function1 ';             function Inner1 () {                // does not have Var, which is equivalent to making a declaration outside of this function             }            inner1 ();            Echo (pscope1, x);        }        Function1 ();

The result of the final print is "in Inner1"

function function2 () {            var x = ' in function2 ';             function Inner2 () {                echo (pscope1, x);                  var // plus var                 Echo (pscope1, x);             }            Inner2 ();  Echo (pscope1, x);          }        function2 ();

The first echo prints "undefined", the second echo prints "in Inner2", and the third echo prints "in function2".

The above inner2 is equivalent to the following code:

  function        inner2 () {                var  x                // at this time X has not yet defined                x = ' In Inner2 ';                 Echo (pscope1, x);             }

There may be some problems with the understanding of the scope chain, and you are welcome to correct it.

Demo Download:

http://download.csdn.net/detail/loneleaf1/7983577

Resources:

http://www.laruence.com/2009/05/28/863.html JavaScript Scope principle

http://www.cnblogs.com/lhb25/archive/2011/09/06/javascript-scope-chain.html JavaScript Development Advanced: Understanding JavaScript scopes and scope chains

http://www.cnblogs.com/zxj159/archive/2013/05/30/3107923.html JavaScript anonymous function (mimic block-level scope)

Http://www.web-tinker.com/article/20331.html "pseudo-block scope" for Try-catch statements

Http://msdn.microsoft.com/zh-cn/library/bzt2dkta%28v=vs.94%29.aspx variable scope (JavaScript)

Scope of http://www.cnblogs.com/rubylouvre/archive/2009/08/21/1551270.html JavaScript variables

http://www.nowamagic.net/librarys/veda/detail/896 JavaScript Variable Scope

JavaScript scope principle (i)--scope chain

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.