Add the implementation code for the method to the JavaScript type (add functionality) _javascript tips

Source: Internet
Author: User
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.