Here are the various types of JS functions I have seen and the invocation, although some of the wording and its invocation I do not know what the jargon is called, but does not affect me to write a summary note.
We are just beginning to touch the JS voice, often see this is called "Use the function keyword to define functions," the wording, such as:
function f (e) {
Alert (e);
}
F ("Hello World");
Assigning a function to a variable is probably called "expression-defined function", such as:
var f=function (e) {
Alert (e);
}
F ("Hello World");
As above, if the function name is omitted, this is also called "anonymous function", of course, can not be anonymous, to a function name, which is used in the recursive function, such as:
var fact=function facttemp (n) {
if (n<=1) return 1;
else return n*facttemp (n-1);
}
Fact (3); Return 6
There is also a function called "using constructors" to define functions, such as:
var f=new Function ("x", "Y", "alert (x*y);");
f (2,3); ==>6
These are the three types of definition functions and their invocations, but there are other ways to invoke them:
Called indirectly using the call () function, such as:
functionPerson (name) { This. name=name; This. sayhi=function(age,blogs) {alert ("My name is:" + This. name+ "; \ n Age:" +age+ "; \ n Blogs:" +blogs); }}functionMe (name) { This. name=name; }varperson1=NewPerson (' function person ');varme1=NewMe ("Function Me");p Erson1.sayHi.call (me1,"http://www.cnblogs.com/xiaomou2014");//originally me1 is no Sayhi function, after using call to invoke the personal in the Sayhi function.
Similarly to the same function as call, apply can also implement such a function, but his second argument is an array, such as:
functionPerson (name) { This. name=name; This. sayhi=function(age,blogs) {alert ("My name is:" + This. name+ "; \ n Age:" +age+ "; \ n Blogs:" +blogs); }}functionMe (name) { This. name=name; }varperson1=NewPerson (' function person ');varme1=NewMe ("Function Me");p erson1.sayHi.apply (me1,["http://www.cnblogs.com/xiaomou2014"]);
A function can assign a value to a variable, and you can assign it directly to an object's properties, such as:
var o={square:function (x) {return x*x;}};
var y=o.square (2); y=4;
Sometimes we have a function that we need him to run immediately, which seems to be quite common in JS plug-ins, such as:
(function () {
Alert ("Hello World");
})();
You can also give him a function name:
(function f () {
Alert ("Hello World");
})();
The second type:
(function () {
Alert ("Hello World");
}());
Similarly, we can give him a function name:
(function f () {
Alert ("Hello World");
}());
Of course you can also give it a parameter:
(function (e) {
Alert (e);
} ("Hello World");
Look at other people's plug-ins, you will find someone else at the beginning of a ";", so that even if the page JS error, loading and running his plugin can also be guaranteed to run, such as:
;(function (e) {
Alert (e);
} ("Hello World");
If a function is a lot of arguments, then we call the function is not very good to remember his order, the parameters are encapsulated into an object, and then the object in the corresponding to the parameters, which is a good way to solve the problem, such as:
1 varf=function(args) {2Sayhi (Args.country | | "Chinese",3 Args.name,4 Args.qq,5 Args.phone,6 args.email)7 }8 functionSayhi (country,name,qq,phone,email) {9Alert ("Hi, I am a" +country+ ", My name is" +name+ "; QQ:" +qq+ ";p Hone:" +phone+ "; Email:" +email); Ten } OneF ({name: "Xiao", Phone: "13888888888", email: "[email protected]", qq:123456});
so as long as the name of the parameter to remember it, do not control his order, while giving the need to assign the default parameters are also very convenient, such as Args.country | | "Chinese", if the function is called without giving country this parameter argument, then his default value is Chinese.