This article mainly introduces the Javascipt function () functions of the use of the tutorial, is javascipt introductory learning in the basic knowledge, the need for friends can refer to the
A function statement is not defined as a new function, and you can define the only way in which your functions are used to construct the use of the operator dynamically.
Note: This is the terminology of object-oriented programming. The first time may feel not very accustomed, here is no problem.
Grammar
The following is the use of the new operator to create a syntax that uses functional function () constructs.
?
1 2 3 4 5 |
<script type= "Text/javascript" > <!--var variablename = new Function (ARG1, Arg2 ..., "Function body"); --> </script> |
function () functions construct any number of expected string parameters. The last argument is the function body-it can contain arbitrary JavaScript statements, separated from each other by semicolons.
Note that the function () construct does not use any arguments, and specifies a name for it to create functions. An unnamed function created using the function () constructor is called an anonymous function.
Example:
Here is an example of creating a function:
?
1 2 3 4 5 |
<script type= "Text/javascript" > <!--var func = new Function ("X", "Y", "return x*y;"); --> </script> |
This line of code creates a new function that is more or less equivalent to the syntax of a defined function:
?
1 2 3 4 5 6 7 |
<script type= "Text/javascript" > <!--function f (x, y) {return x*y;}//--> </script> |
This means that you can call the above function as follows:
?
1 2 3 4 5 |
<script type= "Text/javascript" > <!--func (10,20); This would produce//--> </script> |