Summary and practice of javascript prototype chain diagram

Summary and practice of javascript prototype chain diagram

Prototype chain

The class keyword was introduced in ES6, but JS is still prototype-based, and class is actually syntactic sugar.

For example, there is a people class:

class People {
  constructor(props) {
    this.name = props.name;
  }
  run() {
    console.log('run')
  }
}

Through the new people class, many people are generated, such as Zhang San and Li Si:

let lisi = new People('李四')

But among the vast sea of ​​people, there is a group of people with extraordinary talents. They are called superheroes.

class Hero extends People {
   constructor(props) {
    super(props);
    this.power = props.power
   }
   heyHa() {
       alert(this.power)
   }
}

class Hero inherits People, so the hero is first an individual with a run method, and then Hero has a superpower heyHa method that ordinary people do not have. We can define a hero called Jinx, who has the superpower of cannon:

let Jinx = new Hero({ name: 'jinx', power: 'cannon!' })

Although the instance Jinx does not define a run method, the prototype chain can be used to find that the People prototype has this run method, that is, its implicit prototype __proto__ points to the prototype of the constructor.

insert image description here

When an instance accesses a method or property, it starts with itself and then searches back up the prototype chain.

Jinx.heyHa() // cannon!
// When it has this method Jinx.run = () => console.log('i just fly!')
Jinx.run() // i just fly!

So where does People.prototype.__proto__ point to? Object.prototype , enter the code in the console and you can see:

insert image description here

But __prototype__ of Object.prototype is equal to null, and the end of the universe is nothingness. .

insert image description here

So far, the complete prototype chain diagram is like this:

insert image description here

We can implement a simple JQuery library based on the prototype chain

class JQuery {
  constructor(selector, nodeList) {
    const res = nodeList || document.querySelectorAll(selector);
    const length = res.length;
    for (let i = 0; i < length; i++) {
      this[i] = res[i]
    }
    this.length = length;
    this.selector = selector;
  }
  eq(index) {
    return new JQuery(undefined, [this[index]]);
  }
  each(fn) {
    for(let i = 0; i < this.length; i ++) {
      const ele = this[i]
      fn(ele)
    }
    return this;
  }
  on(type, fn) {
    return this.each(ele => {
      ele.addEventListener(type, fn, false)
    })
  }
  // Extend other DOM APIs
}
const $$ = (selector) => new JQuery(selector);
$$('body').eq(0).on('click', () => alert('click'));

Run it in the console and the result is as follows:

insert image description here

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Do you know Javascript prototype and prototype chain?
  • How much do you know about JavaScript's constructor, prototype, prototype chain and new?
  • In-depth understanding of javascript prototype and prototype chain
  • An article to help you understand Js inheritance and prototype chain
  • JavaScript two pictures to understand the prototype chain
  • Detailed explanation of JavaScript prototype and prototype chain

<<:  Implementation code for infinite scrolling with n container elements

>>:  Detailed explanation of how to solve the position:fixed fixed positioning offset problem

Recommend

Specific use of MySQL segmentation function substring()

There are four main MySQL string interception fun...

How to use docker to deploy spring boot and connect to skywalking

Table of contents 1. Overview 1. Introduction to ...

Architecture and component description of docker private library Harbor

This article will explain the composition of the ...

JavaScript Canvas implements Tic-Tac-Toe game

This article shares the specific code of JavaScri...

Method of Vue component document generation tool library

Table of contents Parsing .vue files Extract docu...

Tips for viewing text in Linux (super practical!)

Preface In daily development, we often need to pe...

Summary of MySQL time statistics methods

When doing database statistics, you often need to...

MySQL 8.0.23 installation and configuration method graphic tutorial under win10

This article shares the installation and configur...

About the pitfalls of implementing specified encoding in MySQL

Written in front Environment: MySQL 5.7+, MySQL d...

Use non-root users to execute script operations in docker containers

After the application is containerized, when the ...

Detailed installation and use of RocketMQ in Docker

To search for RocketMQ images, you can search on ...

Detailed code for implementing 3D tag cloud in Vue

Preview: Code: Page Sections: <template> &l...

Alibaba Cloud ESC Server Docker Deployment of Single Node Mysql

1. Download the accelerated version of msyql dock...