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

How to generate Hive table creation statement comment script in MySQL metadata

Preface This article mainly introduces the releva...

Things to note when designing web pages for small-screen mobile devices

The reason is that this type of web page originate...

Code comment writing standards during web page production

<br />I have summarized the annotation writi...

MySQL complete collapse: detailed explanation of query filter conditions

Overview In actual business scenario applications...

JavaScript to show and hide the drop-down menu

This article shares the specific code for JavaScr...

MySQL grouping queries and aggregate functions

Overview I believe we often encounter such scenar...

Implementation of Vue counter

Table of contents 1. Implementation of counter 2....

Nginx uses reverse proxy to implement load balancing process analysis

Introduction Based on docker container and docker...

MySQL optimization solution: enable slow query log

Table of contents Preface Setting up slow query l...

How many ports can a Linux server open at most?

Table of contents Port-related concepts: Relation...

Web page HTML ordered list ol and unordered list ul

Lists for organizing data After learning so many ...

Using vue3 to implement counting function component encapsulation example

Table of contents Preface 1. The significance of ...