JavaScript Class extends 关键词
- 上一页 constructor()
- 下一页 static
- 返回上一层 Manu Bincike JavaScript Class
定义和用法
extends
Keywords are used to create a subclass (parent) of another class.
The subclass inherits all 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()
The 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 ɗanin
JavaScript 教程:JavaScript ES6 (EcmaScript 2015)
JavaScript 参考手册:super 关键词
JavaScript 参考手册:constructor() 方法
- 上一页 constructor()
- 下一页 static
- 返回上一层 Manu Bincike JavaScript Class