JS's scope chain and closure

Source: Internet
Author: User

1.JS no block-level scope

    <Script>        functionMain () {if (1==1){                varname= "Alex";        } console.log (name);    } Main (); </Script>
Execution result: {} is a block-level scope.
Alex

2.JS takes a function as a scope chain

 <  script         >  function   Main () { var   Innervalue  =  "  alex  "  ;        } Main ();    Console.log (Innervalue);  </ script  > 
Execution Result:
Uncaught Referenceerror:innervalue is not defined

The scope chain for 3.JS has been generated before it was created

Example one:

    <Script>XO= "Alex"; functionFunc () {varXO= "Seven"; functioninner () {console.log (XO); }            returninner; }        varret=Func ();    RET (); </Script>
Execution result: The generated scope chain is XO ("Alex")----xo ("undefined")----XO (using the scope chain)
Seven

Example two:

    <Script>XO= "Alex"; functionFunc () {varXO= "Eric"; functioninner () {console.log (XO); } XO= "Seven"; returninner; }        varret=Func ();    RET (); </Script>
Execution result: The generated scope chain XO ("Alex")----xo ("undefined")----xo ("undefined")----Func[xo] ("Eric")----Func[xo] ("seven")----inner ()
Seven

Example three:

    <Script>XO= "Alex"; functionBar () {console.log (XO); }        functionFunc () {varXO= "Seven"; returnBar; }        varret=Func ();    RET (); </Script>
Execution result: The scope chain is divided into two segments: 1.xo ("Alex")----XO
2.xo ("Alex")----xo ("undefined")----Func[xo] ("seven")
Alex

Closures: If each of the downloaded JS has a custom global variable, then the import will occur when the mutual coverage of the phenomenon, through the closure can be locked into the function of the variable.

(function () {    var a = 123;    Function F1 () {        console.log (a);    }    function F2 () {        cocnsole.log (a);    }}) ();    

Object-oriented JS

    <Script>        functionFoo (name,age) { This. Name=name;  This. Age=Age ; } Foo.prototype={GetInfo:function(){                return  This. Name+  This. Age; }, Func:function(ARG) {return  This. Name+Arg; }        }        varobj= NewFoo ('Alex', A);        Console.log (Obj.name); Console.log (obj.        GetInfo ()); Console.log (obj. Func ( -)); </Script>
Execution Result:
Alexalex22alex55

JS's scope chain and closure

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.