JavaScript Class extends 关键词
- Previous Page constructor()
- Next Page static
- Go Back to Previous Level Manwal ng Reference ng JavaScript Class
定义和用法
extends
Keywords are used to create a subclass (parent) of another class.
The subclass inherits all the methods of another class.
Inheritance is very useful for code reusability: when creating a new class, reuse the properties and methods of existing classes.
Note:From the above example, we can see that,super()
This method refers to the parent class. By calling super()
Methods, we will call the parent class constructor and can access the properties and methods of the parent class.
实例
Create a class named "Model" that will inherit the methods of the "Car" class:
class Car { constructor(brand) { this.carname = brand; } present() { return 'I have a ' + this.carname; } } class Model extends Car { constructor(brand, mod) { super(brand); this.model = mod; } show() { return this.present() + ', it is a ' + this.model; } } mycar = new Model("Ford", "Mustang"); document.getElementById("demo").innerHTML = mycar.show();
语法
class childClass extends parentClass
技术细节
JavaScript 版本: | ECMAScript 2015 (ES6) |
---|
浏览器支持
关键词 | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
extends | 49.0 | 13.0 | 45.0 | 9.0 | 36.0 |
相关页面
JavaScript 教程:JavaScript Class
JavaScript 教程:JavaScript ES6 (EcmaScript 2015)
JavaScript Reference Manual:super Keyword
JavaScript Reference Manual:constructor() Method
- Previous Page constructor()
- Next Page static
- Go Back to Previous Level Manwal ng Reference ng JavaScript Class