Although in peacetime seemingly, very accustomed to some usage but really want to understand these concepts, it really took a lot of effort, now although can not be said to understand but finally have some experience. All right, let's go straight down.
Note This article (*) is a related link
Example 1.1
Lexical scope rules: the nested relationship of a function is determined when it is defined, not when it is called, that is, the lexical scope, that is, the nested relationship is determined by the lexical analysis, and the runtime is determined.
(*)http://blog.csdn.net/zzulp/article/details/8144520
< Span style= "FONT-SIZE:16PX; Color: #ff0000; " > (*) http://www.cnblogs.com/lhb25/archive/2011/09/06/ javascript-scope-chain.html
(*) http://www.jb51.net/article/24101.htm
1 varOa=3;2 3 functionA ()4 {5 varOA = 2;6 returnb ();7 functionB ()8 {9 return function()Ten { One returnOA; A }; - } - } theAlert (A () ());//=>2
Because 11 lines return OA is defined in the scope of the A () function, and when it executes here, OA will look for the active object generated in the scope chain if it is not found, it will look down and find the global variable, but here the A () function Have redefined OA so
First found the <2>.
1.2
1 varOa=3;2 3 functionA ()4 {5 varOA = 2;6 returnb ();7 functionB ()8 {9 return function()Ten { One return This. OA; A }; - } - } theAlert (A () ());//=>3
The This.oa here are returned. (Personal opinion: This.oa is considered to be an attribute under an object, in the scope chain only when the search to the global object is the OA is a property under window so set)
1.3
1 varOa=3;2 3 functionA ()4 {5 varOA = 2;6 returnb ();7 functionB ()8 {9 returnOA;Ten } One } AAlert (A ());//=>2
1.4
1 varOa=3;2 3 functionA ()4 {5 varOA = 2;6 returnb ();7 functionB ()8 {9 return This. OA;Ten } One } AAlert (A ());//=>3
Example 2.1
(*) http://www.w3school.com.cn/js/pro_js_object_scope.asp
(*) http://blog.cnbang.net/tech/2025/
1 varOa=3;2 varA = {3Oa:2,4D |function()5 {6 return function()7 {8 returnOA;9 };Ten } One }; AAlert (A.B () ());//=>3
OA here (the personal understanding is: First this OA is not a property, and OA does not declare VAR so the direct return is the global variable, in order to prove this I declare var oa inside this function; try structure return undefined)
2.2
1 varOa=3;2 varA = {3Oa:2,4D |function()5 {6 return function()7 {8 return This. OA;9 };Ten } One }; AAlert (A.B () ());//=>3
(Personal understanding is: Because when the returnof this. OA points to window, I try to add a.oa=10 to this method and the outer layer of this method, but still return 3, it seems that there is no scope relationship, but
Try again to change 11 lines to return a. OA =>2; explains that there is a relationship between the chain and this is only when this returns to a new scope chain, so the point of this has changed to point to the window )
2.3
1 varOa=3;2 varA = {3Oa:2,4D |function()5 {6 return This. OA;7 }8 };9Alert (A.B ());//=>2
In this example, the B method of a is returned, so this points to a
2.4
1 var oa=3 2 var a = 3 oa:2, 4 B: function () 5 { 6 return OA; 7 8 }; 9 alert (A.B ()); // =>3
Return global variables directly
javascript-lexical scope rules-scope chain-closure application learning experience