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