The order in which objects call methods:If the method in the instance does not exist, look for it in the prototype object of the constructor that created the instance object. We can add methods to the prototype of the system object, which is actually equivalent to changing the source code. Add a method to output a string in reverse orderI wish there was a way to reverse the order of a string. String.prototype.myReverse = function () { for(var i=this.length-1;i>=0;i--){ console.log(this[i]); } }; var str="abcdefg"; str.myReverse(); We can see the output Writing your own Array sorting methodAdd methods to the prototype object of the Array built-in object Array.prototype.mySort = function () { for(var i=0;i<this.length-1;i++){ for(var j=0;j<this.length-1-i;j++){ if(this[j]<this[j+1]){ var temp = this[j]; this[j]=this[j+1]; this[j+1]=temp; }//end if }// end for }//end for }; var arr = [100,3,56,78,23,10]; arr.mySort(); console.log(arr); All case codes <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>title</title> <script> //Add prototype methods to built-in objects var arr = new Array(10,20,30,40,50); arr.join("|"); console.dir(arr); var str = new String ("Oh, that's awesome"); str.indexOf("Oh"); console.dir(str); var dt = new Date(); dt.getFullYear(); console.dir(dt); //If there is no method in the instance, look for it in the prototype object of the constructor that created the instance object. //Can we add a method to the prototype of the system object, which is equivalent to changing the source code. //I hope there is a method to reverse the string in the string. String.prototype.myReverse=function () { for(var i=this.length-1;i>=0;i--){ console.log(this[i]); } }; var str="abcdefg"; str.myReverse(); //Add method to the prototype object of the Array built-in object Array.prototype.mySort=function () { for(var i=0;i<this.length-1;i++){ for(var j=0;j<this.length-1-i;j++){ if(this[j]<this[j+1]){ var temp = this[j]; this[j]=this[j+1]; this[j+1]=temp; }//end if }// end for }//end for }; var arr = [100,3,56,78,23,10]; arr.mySort(); console.log(arr); String.prototype.sayHi = function () { console.log(this+"Haha, I'm handsome again"); }; //The string now has a way to say hello var str2="Xiao Yang"; str2.sayHi(); </script> </head> <body> </body> </html> This is the end of this article about how to add prototype methods to built-in objects in JavaScript. For more information about how to add prototypes to built-in objects in JavaScript, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to install docker on Linux system and log in to docker container through ssh
>>: How to create a trigger in MySQL
Preface This article mainly introduces the releva...
The reason is that this type of web page originate...
<br />I have summarized the annotation writi...
Overview In actual business scenario applications...
This article shares the specific code for JavaScr...
Overview I believe we often encounter such scenar...
Table of contents 1. Implementation of counter 2....
As we all know, the CSS position absolute is set ...
Introduction Based on docker container and docker...
Copy code The code is as follows: <!DOCTYPE ht...
Table of contents Preface Setting up slow query l...
Table of contents Port-related concepts: Relation...
Lists for organizing data After learning so many ...
Current demand: There are two tables, group and f...
Table of contents Preface 1. The significance of ...