In addition to the function object, there are many internal objects, such as Object, Array, Date, RegExp, Math, and Error. These names actually represent a type, and you can return an object by using the new operator. However, unlike other objects, the function object will still return the string "function" when it typeof the type of a function object, and it will return the string "Object" when typeof an array object or other object. The following code example typeof different types of cases:
The following is a reference fragment:
Alert (typeof (Function)));
Alert (typeof) (New Function ());
Alert (typeof (Array));
Alert (typeof (Object));
Alert (typeof) (New Array ());
Alert (typeof (new Date));
Alert (typeof) (New Object ());
Running this code will reveal that the first 4 statements will display "function", and the following 3 statements show "Object", which shows that the new function actually returns a function. This is a lot different from other objects. Other types array, object, and so on will return a normal object via the new operator. Although the function itself is an object, it differs from a normal object because it is also an object constructor, that is, a function can be used to return an object, as described earlier. All TypeOf objects that return "function" are function objects. The object is also called a constructor (constructor), so all constructors are objects, but not all objects are constructors.
Since the function itself is also an object, their type is a function, Lenovo to the C + +, Java, and other object-oriented language class definition, you can guess the role of the function type, that is, you can give the function object itself to define some methods and properties, The prototype object of a function makes it easy to modify and extend the definition of a function type, for example, by extending the functional type functions, adding a method1 method to the pop-up dialog box that displays "function":
The following is a reference fragment:
Function.prototype.method1=function () {
Alert ("function");
}
function Func1 (a,b,c) {
return a+b+c;
}
Func1.method1 ();
Func1.method1.method1 ();
Note the last statement: FUNC1.METHOD1.MEHOTD1 (), which calls the Method1 method of method1 this function object. While it may seem a little confusing, it is clear that a careful look at the syntax: This is a recursive definition. Because the method1 itself is also a function, it also has the properties and methods of the function object, all of which have such a recursive property for the extension of the method of the functions 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 to allow all objects to have some common properties and methods, and modifying the object type is done through prototype:
The following is a reference fragment:
Object.prototype.gettype=function () {
Return typeof (This);
}
var array1=new Array ();
function Func1 (a,b) {
return a+b;
}
Alert (Array1.gettype ());
Alert (Func1.gettype ());
The above code adds a GetType method to all objects, which is to return the type of the object. The two alert statements display "Object" and "function" respectively.
Passing a function as a parameter
In the previous introduction of the function object essence, each function is represented as a special object, you can easily assign it to a variable, and then through the variable name for function calls. As a variable, it can be passed as a parameter to another function, as described earlier in the JavaScript event-handling mechanism, for example, the following program passes FUNC1 as a parameter to FUNC2:
The following is a reference fragment:
function Func1 (thefunc) {
Thefunc ();
}
function Func2 () {
Alert ("OK");
}
Func1 (FUNC2);
In the last statement, Func2 is passed as an object to the FUNC1 's formal parameter thefunc, which is then invoked by Thefunc inside the func1. In fact, passing a function as a parameter, or assigning a function to another variable, is the basis of all event mechanisms.
For example, if you need to do some initialization while the page is loading, you can define an INIT initialization function and then bind it to the page load-completed event by using the Window.onload=init statement. Init here is a function object that can be added to the list of onload events for Windows.