Add common methods to classes in JavaScript

Source: Internet
Author: User
First of all, JavaScript does not support function overloading. If multiple function names on the right are the same, it calls the nearest function, that is, the last function, this JS does not support function overloading and requires special attention. Define a function or variable directly. They... SyntaxHighlighter. all ();

First of all, JavaScript does not support function overloading. If multiple function names on the right are the same, it calls the one closest to him, that is, the most

This JS does not support function overloading.

Define a function or variable directly. They belong to global functions or global variables. They are essentially window objects.

Then we can provide them with a general method for the built-in objects in JS, so that they do not need to be specifically written.

The Code is as follows:

[Javascript]
// We can add methods to the class
Var I = new Number (10 );
Number. prototype. add = function (){
Return this +;
}

Window. alert (I. add (20). add (30 ));
// We can add methods to the class
Var I = new Number (10 );
Number. prototype. add = function (){
Return this +;
}
 
Window. alert (I. add (20). add (30 ));
In this way, we can add a method to the Number object, and we can use it directly.

Let's look at another code.

[Javascript]
Array. prototype. find = function (val ){
For (var I = 0; I <this. length; I ++ ){
If (this [I] = val ){
Window. alert ("subscript" + I );
Return;
}
}
Window. alert ("no ");
}
 
Var t = new Array (3 );
T [0] = 3;
T [1] = 5;
T [2] = 6;
 
T. find (4 );
T. find (5 );
Array. prototype. find = function (val ){
For (var I = 0; I <this. length; I ++ ){
If (this [I] = val ){
Window. alert ("subscript" + I );
Return;
}
}
Window. alert ("no ");
}
 
Var t = new Array (3 );
T [0] = 3;
T [1] = 5;
T [2] = 6;
 
T. find (4 );
T. find (5 );
This provides a general method for the Array object and applies the this keyword. this prototype can provide a method for something equivalent to a class. Mark it

 

From a352193394
 

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.