JavaScript Note 5: function expressions

Source: Internet
Author: User

JavaScript Note 5: function expressions

1. function expressions

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 function being executed, so it can be used for recursion.

Function factorial (num ){

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

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

}

3. Closure: Refers to a function that has the right to access the variables in another function scope. The common form of creating a closure is to create another function within one function.

In the background execution environment, the scope of a closure includes its own scope, function scope, and global scope.

When a function returns a closure, the function scope will remain in the memory, knowing that the closure does not exist.

4. Private variables: Any variables defined in the function can be considered as variables because they cannot be accessed outside the function, private variables include function parameters, local variables, and other functions defined within the function.

JavaScript does not have the formal concept of private object attributes, but you can use closures to implement public methods and access variables in its scope through public methods.

The public method that has the permission to access private variables is called the privileged method.

Creating a closure must maintain an additional scope. Excessive use of the closure may occupy a large amount of memory.

 

Http://www.bkjia.com/kf/201412/359001.html previous

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.