There are three ways to create a function and the concepts of recursion, nesting, and closure are often confusing.
Three ways to define a function: 1. Declarative functions (Standard keyword function+ function name + (argument list) +{function body})
Example: function functionname (param1,param2,......, Paramn)
{Function Statment}
2. Anonymous function (variable assigned by function + constructor function+ (parameter))
For example: Var variable=new function ("param1", "param2",......, "Paramn", "function body");
3. Function literal: will only be parsed by the browser once (both anonymous and non-anonymous)
A. Anonymous form (can be passed as a parameter to another function)
var func=function (param1,param2,......, paramn) {function Satament}
B. Non-anonymous (within the function, only code inside the function can call it by its name, which is perfect for recursion)
var func=function functionname (param1,param2,......, paramn) {function statment}
Understanding of recursion, nesting, and closures:
1. Recursion: Generally used for non-anonymous form function literals
The function calling itself is called a recursive function;
Cons: Recursion consumes more memory and resources, and is difficult to implement and maintain.
Pros: When working with tree-structured data such as the DOM, recursion is a great fit.
Example: function Commonfunction ()
{
var myfunc=function specialfunction (param1,param2,......, Paramn)
{
function body;//There is a condition of judgment that causes recursion to eventually jump out of the loop
Return specialfunction (param1,param2,......, paramn);
}
var result=myfunc (param1,param2,......, paramn);
}
2. Nesting: Not available for anonymous functions (dynamic), because anonymous functions need to be refactored each time they are used.
There is another function inside a function that will form a nesting. For nested functions, intrinsic functions execute within the scope of an external function and have access to parameters and variables for external functions. The external function does not have permission to access the intrinsic function.
An intrinsic function is called directly when passed to an application through an external function, and it can use a parameter value passed as a parameter to an external function.
Example: <script>
function Outerfunc (base)//External function
{
var outerstr= "!!!!";
return function (EXT) {//intrinsic function
return base+ext+outerstr;
}
}
function MyApplication ()//Application functions
{
var basestr=outerfunc ("Hello");//create access to intrinsic functions
var newstr=basestr ("World");
alert (NEWSTR);
}
</script>
3. Closures: The closure can be understood on a nested basis, when the inner function in the nested is a function literal created in the form of a. Internal object, B. and return it to a variable in the keynote application by calling an external function, which forms a javascript closure.
Application: Used to get the value of a variable in an intrinsic function. Because external function A does not have access to variables and parameters in internal function B, in order to be able to use the arguments or variables in the intrinsic function B, we can create a function literal C in the form of an object in intrinsic function B, so that the new function literal C can access the variables and parameters in the inner function B. By assigning an intrinsic function B to a variable in an external function A, you can simulate the function literal C by using this variable in the external function A to achieve access to the parameters and variables of the intrinsic function B.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
javascript--recursion, nesting, and closure