Add method implementation code for JavaScript type (add function)

Source: Internet
Author: User

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 exampleCopy 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 exampleCopy 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

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.