function
In JavaScript, a function is also a data type, and there are two ways to define a function:
function f () {return 1;}
var f=function () {return 1;}
So, the function in JavaScript is a kind of data, but it has two important features: it contains code that is executable
A function's naming convention is the same as a generic variable, and cannot begin with a number and can consist of arbitrary letters, numbers, underscores, and anonymous functions .
A piece of data is neither assigned to a variable nor given any name is anonymous. Two ways to use anonymous functions:
-Pass anonymous functions as arguments to other functions, and the receiver function can use the anonymous function passed over to complete the corresponding task
-Define an anonymous function to perform some one-time task callback functions
Defines a function that takes two functions as arguments, which perform the two parameter functions, respectively:
Defines a function that takes two functions as arguments, which execute the two parameter functions and return their return values and function
Invoke_and_add (a,b) {returns
a () + B ();
}
Simple definition of function one
() {return
1;
}
for participating operations function two () {return
2;
}
Pass these two functions to the target function
Invoke_and_add (one, two);
Here we can use the anonymous function instead of the above two functions to participate in the operation, as the parameters of the target function:
function Invoke_and_add (a,b) {return
a () + B ();
}
Invoke_and_add (function () {return 1;},function () {return 2;})
When we pass function A to function B, and B performs a, A is a callback function (callback functions), and if A is a nameless function at this point, it is called an anonymous callback function. The advantage of the callback function is to pass the function without naming, which means that you can save the global variable from delegating a function call to another function, saving the code to provide a high performance callback example
The return value of a function is generally passed to another function.
Multiplies the three parameters received by a loop by 2, and returns the result
function Multiplybytwo (a,b,c) {
var i,ar =[] as an array;
for (i=0;i<3;i++) {
ar[i] = arguments[i]*2;}
return ar;
}
Accepts only one argument, adds it 1 and returns
function AddOne (a) {return
a+1;
}
Multiplybytwo (1,2,3);
AddOne (100);
Next, we implement the three elements passing between two functions, defining an array to store the elements, starting with the call to Multiplybytwo ()
var Myarr = [];//defines an array
Myarr = multiplybytwo (10,20,30);//Call function
Then, iterate through each element and pass them to the AddOne ()
for (Var i=0;i<3;i++) {
Myarr[i] = AddOne (Myarr[i])
}
Myarr
In the process above, two loops are used, which can be improved by merging them together, instead of receiving a callback function for the Multiplybytwo () function, and calling it in each iteration operation:
function Multiplybytwo (a,b,c,callback) {
var i,ar =[];
for (i=0;i<3;i++) {
Ar[i] = callback (arguments[i]*2);
}
return ar;
}
function AddOne (a) {return
a+1;
}
Myarr = Multiplybytwo (1,2,3,addone);
We can also use anonymous functions instead of AddOne (), which saves an extra global variable
function Multiplybytwo (a,b,c,callback) {
var i,ar =[];
for (i=0;i<3;i++) {
Ar[i] = callback (arguments[i]*2);
}
return ar;
}
Myarr = Multiplybytwo (1,2,3,function (a) {return a+1});
self-tuning function
Another application of the anonymous function, which can be invoked by itself after the definition.
(
function () {
alert (' Boo ');
}
) ()
You just put the definition of an anonymous function in a stack of parentheses, and then the outside is followed by a pair of parentheses, and the second parenthesis means "call now", and it's also where you pass parameters to anonymous functions.
(
function (name) {
alert (' Hello ' + name + '! ');
}
) (' Dudu ')
Internal private Functions
Define another function inside one function.
function A (param) {
function B (theinput) {return
theinput*2;
};
Return ' The result is ' + B (param);
};
or using the function identifier notation:
var a = function (param) {
var b = function (theinput) {return
theinput*2;
};
Return ' The result is ' + B (param);
};
When we call global function A (), the local function B () is also called internally, because B () is a local function that is not visible outside a (), so it is called a private function. Its advantages are: to help ensure the purity of the global namespace (the probability of a naming conflict is small) private sex--you can choose to expose only the necessary functions to the outside, and retain their own functions, so that they can not be used in other parts of the program return function function
A function always has a return value, and an undefined is implicitly returned, even if it is not explicitly returned. Since a function can return a unique value, the value may also be another function:
function A () {
alert (' a! ');
return function () {
alert (' b! ');
}
function A () returns another function B () after the execution of say A, and B () goes on to do something else. B, let's do it. The return value is assigned to a variable, and then it can be called to the same function as a normal one.
function A () {
alert (' a! ');
return function () {
alert (' b! ');
}
var newfunc = A ();
Newfunc ()
As the execution result shows, the first line executes a, the second row is B, and if you want the returned function to execute immediately, you can do it without assigning it to the variable, and then add a pair of parentheses directly to the calling function:
A () ()
rewrite your own functions
A function can return another function, so you can overwrite the old function with a new function, such as the previous example, by overriding the A () function by the return value of a ().
A = a ();
The current sentence still only executes alert ("a!"), but we call A () again, and we execute alert ("b!") Out. We can also let the function rewrite itself from the inside:
function A () {
alert (' a! ');
A=function () {
alert (' b! ');
}
A comprehensive example:
var a = function () {
function Somesetup () {
var setup = ' Done ';
}
function ActualWork () {
alert (' Worky-worky ');
}
Somesetup ();
return actualwork;
} ();
Private functions are used in this function: Somesetup () and ActualWork ()
Using a self-tuning function: function A () is defined with a pair of parentheses followed by a self invocation.
When the function is invoked for the first time, the Somesetup () function is invoked and a reference to the function variable actualwork is returned, noting that the return value is not parenthesized, so the result is merely a reference to a function, and no function calls are performed without executing functions. The statement executed here starts with the var a =, so the value returned by the Self-tuning function is assigned to a