JavaScript function expressions

Source: Internet
Author: User
Tags closure

function declaration

function declaration Promotion

function expression

anonymous function/lambda function

1 recursion

2 closures (functions that have access to variables in another function scope)

2.1 Closures and variables

A closure can only get the last value that contains any variable in the function.

function createfunctions () {    varnew  Array ();      for (var i = 0; i < i++) {        function() {            return  i;< c16/>}    }    return  result;

The results returned 10 of 10;

You can force a closure to behave as expected by creating another anonymous function (but it feels a bit stupid and messy)

function createfunctions () {    varnew  Array ();      for (var i = 0; i < i++) {        function(num)            {return  function() {                return  num;            }     }}}

2.2 About this object

The execution environment of an anonymous function is global, and this points to other objects when the function execution environment is changed by call () or apply ().

var name = "the window"; var object = {    "My object",    function() {        return function () {            returnthis. Name;        }     // "The Window" (in non-strict mode)

2.3 Memory leaks

If an HTML element is stored in the scope chain of the closure, it will cause the element to not be destroyed.

function Assignhandler () {    var element = document.getElementById ("someelement");     var id = element.id;     function () {        alert (ID);    };     // Important!!!    NULL ;}

3 Impersonation block-level scopes

JavaScript does not have the concept of block-scoped scopes.

function Outputnumbers (count) {    for (var i=0; i < count; i++) {        alert (i);    }     // Count }

Anonymous functions can be used to mimic block-level scopes.

(function() {    //  This is a block-level scope });
function () {    // Error! 

4 Private variables

There is no concept of private members in JavaScript, and all object properties are public.

Public methods that have access to private variables and private constructors are called privileged methods.

function MyObject () {    // private variable and private function    var privatevariable = ten;     function privatefunction () {        returnfalse;    };      This function () {        privatevariable++        ; return privatefunction ();    };}

4.1 Static Private variables

(function() {    // private variable and private function    var privatevariable = ten;     function privatefunction () {        returnfalse;    }     function () {    };     function () {        privatevariable++        ; return privatefunction ();    };}) ();

4.2 Module Mode

varSingleton =function() {    //private variables and private functions    varPrivatevariable = 10; functionprivatefunction () {return false; }    return{publicproperty:true, Publicmethod:function() {privatevariable++; returnprivatefunction (); }    };};

4.3 Enhanced module mode

varSingleton =function() {    //private variables and private functions    varPrivatevariable = 10; functionprivatefunction () {return false; }    //Creating Objects    varObject =NewCustomtype (); //Add privilege/public properties and MethodsObject.publicproperty =true; Object.publicmethod=function() {privatevariable++; returnprivatefunction ();    }; //return This object    returnobject;};

JavaScript function expressions

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.