JavaScript adds prototype method implementation for built-in objects

JavaScript adds prototype method implementation for built-in objects

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 order

I 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

insert image description here

Writing your own Array sorting method

Add 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); 

insert image description here

All case codes

insert image description here

<!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:
  • Introduction to JavaScript built-in objects
  • Javascript basics about built-in objects
  • Analysis of the usage of common built-in objects in JavaScript
  • JavaScript built-in object math, global function and usage example analysis
  • Detailed explanation of JavaScript's built-in objects
  • A brief discussion of js's commonly used built-in methods and objects
  • In-depth understanding of JavaScript single built-in objects
  • Detailed explanation of JavaScript built-in object operations
  • A detailed introduction to jsp built-in objects and methods
  • JavaScript built-in object properties and methods collection
  • How to use the built-in object Math of javascript object
  • Introduction to built-in objects in JavaScript

<<:  How to install docker on Linux system and log in to docker container through ssh

>>:  How to create a trigger in MySQL

Recommend

Code to display the contents of a txt book on a web page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...

How to deploy python crawler scripts on Linux and set up scheduled tasks

Last year, due to project needs, I wrote a crawle...

MySQL database JDBC programming (Java connects to MySQL)

Table of contents 1. Basic conditions for databas...

Detailed explanation of MySQL information_schema database

1. Overview The information_schema database is th...

What you need to know about msyql transaction isolation

What is a transaction? A transaction is a logical...

A brief analysis of Linux network programming functions

Table of contents 1. Create a socket 2. Bind sock...

Detailed explanation of HTML page header code example

Knowledge point 1: Set the base URL of the web pa...

An article explains Tomcat's class loading mechanism

Table of contents - Preface - - JVM Class Loader ...

Detailed analysis of binlog_format mode and configuration in MySQL

There are three main ways of MySQL replication: S...

A brief discussion on the principle of React two-way data binding

Table of contents What is two-way data binding Im...

How to configure wordpress with nginx

Before, I had built WordPress myself, but at that...

Tutorial on installing MySQL 5.7.9 using RPM package under CentOS 7

Recorded MySQL 5.7.9 installation tutorial, share...

Cleverly use CSS3's webkit-box-reflect to achieve various dynamic effects

In an article a long time ago, I talked about the...

Sample code for making desktop applications with vue + Electron

1.vue packaging Here we use the vue native packag...