About-javascript-closure Scope records

Source: Internet
Author: User

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

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.