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