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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...
Last year, due to project needs, I wrote a crawle...
Table of contents 1. Basic conditions for databas...
1. Overview The information_schema database is th...
What is a transaction? A transaction is a logical...
Table of contents 1. Create a socket 2. Bind sock...
Knowledge point 1: Set the base URL of the web pa...
Table of contents - Preface - - JVM Class Loader ...
There are three main ways of MySQL replication: S...
Table of contents What is two-way data binding Im...
Preface According to the project needs, Vue-touch...
Before, I had built WordPress myself, but at that...
Recorded MySQL 5.7.9 installation tutorial, share...
In an article a long time ago, I talked about the...
1.vue packaging Here we use the vue native packag...