Home  >  Article  >  Web Front-end  >  Is there inheritance in es6?

Is there inheritance in es6?

WBOY
WBOYOriginal
2022-05-26 14:29:321637browse

There is inheritance in es6. In es6, you can use the extends keyword to inherit any object with a constructor and prototype. You can not only inherit a class, but also inherit an ordinary constructor; constructor is the constructor of the class, which will be automatically called when creating an instance of the class through new. method.

Is there inheritance in es6?

The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.

Is there inheritance in es6

One of the most outstanding new features of ECMAScript 6 is native support for the class inheritance mechanism. Although the class inheritance mechanism uses new syntax, the prototype chain is still used behind it.

ES6 classes support single inheritance. Using the extends keyword, you can extend any object that has [ [ constructor ] ] and a prototype. To a large extent, this means that you can inherit not only a class, but also ordinary constructors.

In ES6, we have syntactic sugar and introduced the concept of class, and in class, there is a default attribute method: constructor. This method is the constructor of the class. This method is automatically called when we create an instance of the class through new.

Methods defined on classes and prototypes will be brought to derived classes.

class Vehicle {
//原型上的方法
identifyPrototype( id ){
console.log( id, this ); // this 实例
}
static identifyClass( id ){
console.log( id, this ); // this Vehicle 类
}
}

Methods of derived classes can reference their prototypes through the super keyword. This keyword can only be used in derived classes, and only inside class constructors, instance methods, and static methods. Use super in a class constructor to call the parent class constructor.

extends keyword, super()

extends means inheriting the parent class, super() means the parent class constructor, and the writing method continues as above:

Is there inheritance in es6?

Then it should be very clear here. Compared with the troublesome changes in this pointer and constructor pointer in ES5, it can be solved by directly using extends and super().

[Related recommendations:javascript video tutorialweb front-end

The above is the detailed content of Is there inheritance in es6?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn