JavaScript type functions (such as Number/String/Boolean/Array/Date/Obejct) are inherited from functions. prototype. adding a method to prototype also affects the underlying type functions derived from it. For example:
Copy codeThe Code is as follows:
Function. prototype. addMethod = function (methodName, func ){
If (! This [methodName]) {
This [methodName] = func; // Add a method to the type, similar to a static method of the type. The func method is assigned to a type rather than an instance.
}
Return this; // this will be bound to the call object (type function) of the method, and this can be returned for chained calls.
}
Array. addMethod ('testfun', function () {alert (this )});
// Array. testFun (); // function Array () {[native code]}
Object. addMethod ('testfun', function () {alert (this )});
// Object. testFun (); // function Object () {[native code]}
Boolean. addMethod ('testfun', function () {alert (this )});
// Boolean. testFun (); // function Boolean () {[native code]}
Function CustomObject (name, value ){
This. name = name | 'customobject ';
This. value = value | 0;
This. toString = function () {return '[name:' + this. name + ', value:' + this. value + ']'}
}
CustomObject. addMethod ('testfun', function () {alert (this )});
/* Return:
* Function CustomObject (name, value ){
This. name = name | "CustomObject ";
This. value = value | 0;
This. toString = function () {return "[name:" + this. name + ", value:" + this. value + "]" ;};
}
*/
CustomObject. testFun ();
If you use an instance to call the API, an error is returned. For example:
Copy codeThe Code is as follows:
Var customObject = new CustomObject (); // defines a CustomObject instance
CustomObject. testFun (); // Error: temp. testFun is not a function
Add method to instance
If a method is added to a type instance, the method should be bound to the Type prototype. For example
Copy codeThe Code is as follows:
Function. prototype. addMethod = function (methodName, func ){
If (! This. prototype [methodName]) {
This. prototype [methodName] = func; // Add a method to the prototype, which affects the type of instances.
}
Return this. prototype; // return the prototype. this type of instance can be called in a chain.
}
Object. addMethod ('testfun', function () {alert (this )});
// ({ToString: function () {return '[Empty Object]'}). testFun (); // [Empty Object]
Number. addMethod ('testfun', function () {alert (this )});
// (5). testFun (); // 5
String. addMethod ('testfun', function () {alert (this )});
// 'Test'. testFun (); // 'test'
Boolean. addMethod ('testfun', function () {alert (this )});
// True. testFun (); // true
Array. addMethod ('testfun', function () {alert (this )});
// (['A', 'B']). testFun (); // a, B
Date. addMethod ('testfun', function () {alert (this )});
// New Date (). testFun (); // Tue Dec 27 2011 11:20:58 GMT-0800 (Pacific Standard Time)
Function CustomObject (name, value ){
This. name = name | 'customobject ';
This. value = value | 0;
This. toString = function () {return '[name:' + this. name + ', value:' + this. value + ']'}
}
CustomObject. addMethod ('testfun', function () {alert (this )});
Var customObject = new CustomObject ();
CustomObject. testFun (); // [name: CustomObject, value: 0]
If testFun is called by type, an error is returned. For example
Copy codeThe Code is as follows:
Array. addMethod ('testfun', function () {alert (this )});
// Array. testFun (); // Error: Array. testFun is not a function
CustomObject. addMethod ('testfun', function () {alert (this )});
CustomObject. testFun (); // Error: CustomObject. testFun is not a function