JavaScript -- recursion, nesting, and closure

Source: Internet
Author: User

JavaScript -- recursion, nesting, and closure

The three methods of function creation and recursion, nesting and closure are often easy to confuse.

Three methods of function Definition: 1. Declarative function (standard keyword function + function name + (parameter list) + {function subject })

Example: function functionname (param1, param2,..., paramn)

{Function statment}

2. Anonymous functions (assign the variable + constructor Function + (parameter) to the Function ))

For example: var variable = new Function (param1, param2,..., paramn, function body );

3. Function literal: Only parsed by the browser once (anonymous and non-Anonymous)

A. Anonymous (it can be passed as A parameter to another function)

Var func = function (param1, param2,..., paramn) {function satament}

B. Non-anonymous (applicable to functions, only the code inside the function can call it by its name, which is very suitable for recursion)

Var func = function functionname (param1, param2,..., paramn) {function statment}

 

Recursion, nesting, and closure:

1. recursion: It is generally used for non-Anonymous functions.

Calling a function is called a recursive function;

Disadvantage: recursion occupies a large amount of memory and resources, which is difficult to implement and maintain at the same time.

Advantage: recursion is ideal for processing tree-like data such as DOM.

Example: function commonFunction ()

{

Var myFunc = function specialFunction (param1, param2,..., paramn)

{

Function body; // a judgment condition exists to make recursion jump out of the loop.

Return specialFunction (param1, param2,..., paramn );

}

Var result = myFunc (param1, param2,..., paramn );

}

2. nesting: it is not applicable to anonymous functions (Dynamic functions) Because anonymous functions need to be reconstructed each time they are used.

If another function exists in a function, nesting is formed. For nested functions, internal functions are executed within the scope of external functions and have access to parameters and variables of external functions. External functions do not have the permission to access internal functions.

An internal function is called directly when it is passed to an application through an external function. It can be used as a parameter to pass the parameter value to an external function.

Example: <script>

Function outerFunc (base) // external function

{

Var outerStr = !!!!;

Return function (ext) {// internal function

Return base + ext + outerStr;

}

}

Function myApplication () // application function

{

Var baseStr = outerFunc (Hello); // create access to internal functions

Var newStr = baseStr (World );

Alert (newStr );

}

</Script>

3. closure: You can understand the closure Based on nesting. When the nested internal function is. A function created in the form of an internal object. and return it to a variable in the main application by calling an external function, which forms a JavaScript closure.

Application: used to obtain variable values in internal functions. Because external function a does not have the permission to access the variables and parameters in internal function B, in order to be able to use the parameters or variables in internal function B, we can create an object-type function literal c in the internal function B, so that the new function literal c can access the variables and parameters in the internal function B. By assigning internal function B to the variable in external function a, you can use this variable in external function a to simulate the literal amount of Function c and access the parameters and variables of internal function B.

 

 

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.