JavaScript Note five: function expressions

Source: Internet
Author: User

1. Function expression

var function = function () {

function body

};

2. Recursion

function factorial (num) {

if (num<=1) {return 1; }

else {return num*factorial (num-1);}

}

Arguments.callee is a pointer to the executing function, so you can use it to implement recursion.

function factorial (num) {

if (num<=1) {return 1; }

else {return Num*rguments.callee (num-1);}

}

3. Closure: Refers to a function that has access to variables in another function scope, and the common form of creating closures is to create another function inside one function.

The scope of the closure in the background execution environment contains his own scope, the scope of the function and the global scope

When a function returns a closure, the scope of the function is always in memory, knowing that the closure does not exist.

4. Private variables: Any variable defined in a function can be considered to be a variable, since these variables cannot be accessed outside the function, and private variables include parameters of the function, local variables, and other functions defined inside the function.

There is no formal concept of private object properties in JavaScript, but you can use closures to implement public methods to access variables in their scope through public methods

Public methods that have access to private variables are called privileged methods

Creating closures must maintain additional scopes, and excessive use may result in large memory consumption.

JavaScript Note five: function expressions

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.