JS want to write some concepts always understand clearly the recent summary of some closures and scope of the case as follows
Example 1.
1 varName = "the window";2 varObject = {3Name: "My Object",4Getnamefunc:function()5 {6 return function()7 {8 return This. Name;9 };Ten } One }; A -Alert (Object.getnamefunc () ());//=>the Window
This is an example of a search on the internet.
Make a try First
var name = "the window"; var object = { "My object", function() { Returnfunction() { returnthis. name; }; } }; Alert (Object.getnamefunc ()); // Watch this .
Returned is the
function () { returnthis. name; };
Then this example is well understood to actually return to
var Object=this. Name//=>this is window
Example 2.
Made a few changes as follows
1 varName = "the window";2 varObject ={3Name: "My Object",4Getnamefunc:function()5 {6 return(function()7 {8 return This. Name;9 })();Ten } One }; A -Alert (Object.getnamefunc ());//=>the Window
1 varName = "the window";2 varObject ={3Name: "My Object",4Getnamefunc:function()5 {6 return(function(_this)7 {8 return_this.name;9})( This);Ten } One }; A -Alert (Object.getnamefunc ())//My Object
The method of anonymous function The second kind should be more common use of the scope of the characteristics of the parameter
Example 3.
Try it yourself.
1 varOa=0;2 3Alert (A () ());//=>24 5 functionA ()6 {7 varOA = 1;8++OA;9 returnB (OA);Ten } One A functionB (OA) - { - return function() the { - returnOA; - }; -}
1 varOa=0;2 3Alert (A () ());//24 5 functionA ()6 {7 varOA = 1;8++OA;9 return(function()Ten { One return function() A { - returnOA; - }; the })(); -}
The example itself is not very clear and a little confused
About-javascript-closure Scope records