1. JavaScript Advanced function Scope chain

Source: Internet
Author: User

Scope chain :

Each of the function functions of JavaScript has its own scope, which is saved using active object (AO) active objects, forming a scope chain in the nested functions.


The scope chain is the AO chain from inside to outside

The search for variables:

Variables used in the function fn3, such as those found within the FN3 scope, are looked up to the outer fn2 scope, and so on, until the Global object window


Code Demo:


var c = 5;function T1 () {    var d = 6;    Function T2 () {        var e = 7;        var d = 3;//if var d = 3 is declared here, then the function is not looking out for the variable d and the output value is        console.log (c+d+e);    }    T2 ();} T1 ();

After understanding the scope chain of JavaScript, it is better to use external variables with higher frequency in the function, it is best to save the external variables as local variables before doing so, which greatly reduces the time to find the variable through the scope chain.



1. JavaScript Advanced function Scope chain

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.