InheritanceES5 prototype inheritance Inheritance is achieved through the prototype chain (constructor + [[prototype]]). (Note: I will write __proto__ in the form of [[prototype]] in the future) // Parent class: function SuperType; Subclass: function SubType; SubType.prototype = new SuperType(); // SubType inherits SuperType // According to the knowledge point mentioned in the previous section on prototype chain: the __proto__ of the instantiated object is equal to the prototype of the constructor SubType.prototype.__proto__ === SuperType.prototype // true The inheritance relationship above is as follows: In terms of internal implementation mechanism, the inheritance of ES5 is actually to first create an instance object of the subclass on this, and then add the parent class method to this this. Similar usage: Father.apply(this) ES6 class inheritance Inheritance is achieved through class's extends + super. Super usage super can be used as a function and an object. The difference between the twoA: They are not exactly the same. There are several main differences:
ES5 prototype inheritance internal implementationInheritance in ES5 is essentially creating an instance object of the subclass this first, and then adding the parent class's method to the subclass (this) --- Father.apply(this). ES6 class inheritance internal implementationThe inheritance mechanism of ES6 is completely different. In essence, it first creates an instance object this of the parent class, puts the properties and methods of the parent class on this (provided that it is called through the super function), and then uses the constructor of the child class to modify this. Because of the different implementation mechanisms, these two types of inheritance have some differences when inheriting native constructors: es5 writing cannot inherit native constructors (such as Array, Number, etc.) ExtensionsFor a description of the internal implementation mechanism, please refer to the relevant section of "Ruan Yifeng's es6 Document - Class Inheritance" This concludes this article about the differences between ES6 inheritance and ES5 inheritance in Java. For more information about the differences between ES6 inheritance and ES5 inheritance, please search 123WORDPRESS.COM’s previous articles or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: A Brief Analysis of Subqueries and Advanced Applications in MySql Database
>>: Implementation of grayscale release with Nginx and Lua
Table of contents 1. Create a stored function 2. ...
Table of contents 1. Phenomenon 2. Solution 3. Su...
The installation and configuration methods of MyS...
This article hopes to gain some insights through a...
1. Problem Sometimes when we log in to Mysql and ...
<br />I am very happy to participate in this...
This article shares the specific steps for config...
Preface Readers who are familiar with MySQL may f...
Let's make a simple 3D Rubik's Cube today...
I have recently been following the CSS Animation ...
IE8 will have multiple compatibility modes . IE pl...
Table of contents Preface 1. Error log 2. Binary ...
Preface This article describes two situations I h...
This article shares the specific code of React+ts...
This article example shares the specific code of ...