three ways to affirm functions (declaring a function)
Method One: function functionname ([parameters]) {functionbody};
Example D1
Copy Code code as follows:
function Add (A, B)
{
return a+b;
}
Alert (Add (1,2)); Produces 3
When we declare a function like this, the contents of the function are interpreted (but not executed, which is performed only if we call the function), and an object named add is already established.
method Two: Assign a function function that is not named to a specified variable (VAR)
Example D2
Copy Code code as follows:
var add=function (A, B)
{
return a+b;
}
Alert (Add (1,2)); Produces 3
The syntax for this kind of affirmation looks weird, but it helps us to better understand "function as object." The contents of a function declared in this way are also interpreted and not executed.
At the same time, we can also define this:
Example D2A:
Copy Code code as follows:
var add=function theadd (A, B)
{
return a+b;
}
Alert (Theadd (1,2)); Produces 3
Alert (Add (1,2)); also produces 3
As can be seen from the example above, we can call a function by theadd the function name, or we can call the function by the name add of the specified variable.
Example D2B:
Copy Code code as follows:
var myobject=new Object ();
Myobject.add=function (a,b) {return a+b};
MyObject now has a property/a method named "Add"
and I can use it like below
Myobject.add (1, 2);
And this example shows that when we need to use a custom function as a property of an object, this method of function declaration is very useful, and it is closer to the idea of OOP.
method Three: Use the new operator to declare a function
Copy Code code as follows:
Varname=new Function ([Param1name, Param2name,... paramnname], functionbody);
Example D3:
Copy Code code as follows:
var add=new Function ("A", "B", "Return a+b;");
Alert (Add (3,4)); Produces 7
Here are two parameters A and B, and a function body, which returns A+B. Also note that the new function (...), which uses the uppercase function rather than the lowercase function, indicates that we are creating a function object (recall the new object () to create an object). At the same time, we can see that the preceding parameter names and subsequent function bodies are passed in string form (note: They all have double quotes). We can have a number of arguments, JavaScript automatically discriminant the function body, usually the closest one to the right parenthesis. Of course, we do not necessarily have to write all the code on the same line, we can write multiple lines, the middle using "+" or "\" for the join. "+" and "\" Tell us that JavaScript is going to look for the rest of the code on the next line.
Example D3A
Copy Code code as follows:
var add=new Function ("A", "B",
"Alert" +//Chop string using "+"
"(' adding ' +a+ ' and ' +b '); \//separate string using" \ "
return a+b; ");
Alert (Add (3,4)); Produces 7
Of course, by declaring the function in this way, the function body is not interpreted (to be interpreted until run time), which results in a decrease in performance. Why, then? Take a look at the following example:
Example d3b
Copy Code code as follows:
function Createmyfunction (myoperator)
{
Return the new Function ("A", "B", "return a" + Myoperator + "B;");
}
var add=createmyfunction ("+"); Creates "add" function
var subtract=createmyfunction ("-"); Creates "subtract" function
var multiply=createmyfunction ("*"); Created "Multiply" function
Test the functions
Alert ("Result of add=" +add (10,2)); Result is 12
Alert ("Result of substract=" +subtract (10,2)); Result is 8
Alert ("Result of multiply=" +multiply (10,2)); Result is 20
alert (add);
This example is more interesting, and it can be executed with three different function objects created by passing parameters (Myoperator) respectively. The interpreter then interprets the "operator" in return in an attempt to explain createmyfunction, which is not clear, so that the performance is naturally discounted.
Of course, when we have special uses, such as allowing users to create their own custom functions, we can use this function statement, but we should try to avoid this use.