In fact, we often use the definition of the function of writing is only one of the writing, there are actually three kinds of writing:
1-Declarative (Static)
2-Object (dynamic)
3 literal (also known as a function expression)
The following is a reference fragment:
The code is as follows |
Copy Code |
function Func1 (...) {...} var func2=function (...) {...}; var func3=function func4 (...) {...}; var func5=new Function (); |
Here are some.
of the 1-declarative
Writing: function func (param1,param2 ...) {...};
This is the most commonly used, it is static, and Java static method is similar to the page when the load is resolved, and only once resolved. I will not elaborate on this, programmers know.
You can define a function by using the function keyword, and specify a function name for each function, which is invoked by the name of the functions. When JavaScript interprets execution, the function is maintained as an object, which is the function object to introduce.
In JavaScript, the type of the function object corresponds to functions, just as the type of the array object is the array, and the Date object corresponds to the type date, and you can create a function object by using the new function (). You can also create an object by using the function keyword. For ease of understanding, we compare the creation of function objects and the creation of arrays of objects. First look at the array object: The following two lines of code are to create an array object myarray:
The following is a reference fragment:
The code is as follows |
Copy Code |
var myarray=[]; Equivalent to var myarray=new Array (); Again, the following two pieces of code create a function MyFunction: function MyFunction (a,b) { return a+b; } Equivalent to var myfunction=new Function ("A", "B", "Return A+b"); |
2 Object-type
By comparing and constructing the Array object statement, you can clearly see the nature of the function object, the function declaration described earlier is the first way of the above code, but inside the interpreter, when this syntax is encountered, a function object is automatically constructed and stored and run as an internal object. As you can see here, a Function object name (function variable) and a normal variable name have the same specification, can be referenced by variable name to this variable, but the function variable name can be followed by parentheses and argument list for function calls.
Creating a function in the form of the new function () is uncommon because a function body usually has multiple statements that, if passed as a string, are not readable by the code. Here's how to use the syntax:
Writing:
The code is as follows |
Copy Code |
var func =new function ("param1", "param2",... "function Body"); |
The following is a reference fragment:
The code is as follows |
Copy Code |
New Function ("A", "B", "C", "Return A+b+c") New Function ("A, B, C", "Return A+b+c") New Function ("A,b", "C", "Return A+b+c") |
Description: The number of parameters of the constructor is uncertain, the first and last formal parameter is the function body, and the other parameters are the formal parameters of the function.
Object type (In fact, this is the name of my life, the book called Dynamic I Feel defective, this writing is clearly in the creation of objects) is dynamic, because its constructor parameters are strings, you can dynamically change. As it is, the function body will have to be parsed every time it is created, and the performance is certainly not as static, but it has its advantages because you can use it to dynamically create a function.
3 literal type
A function is the basis for all function objects, and object is the basis for all objects, including function objects. In JavaScript, any object is an instance of object, so you can modify the type of object so that all objects have some common properties and methods, and modifying object types is done by prototype
Writing: Var func=function [function name optional, function visible] (parms) {...};
Example: Var func=function functest (ID) {alert (ID)}
Description: This type of function name, only visible in the function body, can be used to implement recursive functions.
Literal literals are used for event registration, such as: Obj.onclick=function (this) {alert (this.id)}.
The code is as follows |
Copy Code |
Object.prototype.gettype=function () { Return typeof (This); } var array1=new Array (); function Func1 (a,b) { return a+b; } Alert (Array1.gettype ()); Alert (Func1.gettype ());
|