JavaScript type functions (such as NUMBER/STRING/BOOLEAN/ARRAY/DATE/OBEJCT, etc.) are inherited from the Function.prototype, so adding a method to the Function.prototype also affects the underlying type function derived from it. Such as:
Copy Code code as follows:
Function.prototype.addmethod=function (Methodname,func) {
if (!this[methodname]) {
This[methodname]=func;//adds a method to a type, similar to a static method of a type. The Func method is assigned to a type, not an instance.
}
Return this;//this will bind to the calling object (type function) of the method, which returns this to be called by the chain
}
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 invoke this at this point, you will get an error. Such as:
Copy Code code as follows:
var customobject=new customobject (); Define a CustomObject instance
Customobject.testfun ();//error:temp.testfun is not a function
Add a method to an instance
If you add a method to a type instance, you should bind the method to the prototype of the type. Such as
Copy Code code as follows:
Function.prototype.addmethod=function (Methodname,func) {
if (!this.prototype[methodname]) {
This.prototype[methodname]=func;//adds a method to the prototype that affects the instance of the type
}
Return this.prototype;//Returns the prototype, this type instance can be called by 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 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 you call Testfun with a type at this time, an error is made. Such as
Copy Code code 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