JavaScript ClassextendsKeyword

JavaScript 类(class)JavaScript Class


Examples

Examples

The following example creates a class "Example" that inherits the "Site" class:

class Site { constructor(name) { this.sitename = name; } present() { return 'I like' + this.sitename; } } class Example extends Site { constructor(name, age) { super(name); this.age = age; } show() { return this.present() + ', it was created in' + this.age + 'year.'; } } let noob = new Example("Example", 5); document.getElementById("demo").innerHTML = noob.show();

Try it Yourself »


Definition and Usage

The extends keyword is used to create a class that is a child class of another class.

The child class inherits all methods from another class.

Inheritance is useful for code reusability: reuse the properties and methods of an existing class when you create a new class.

The super() method refers to the parent class's constructor.

By calling the super() method in the constructor, we call the parent class's constructor, so we can access the parent class's properties and methods.

Syntax

class childClass extends parentClass

Technical Details

JavaScript Version: ECMAScript 2015 (ES6)

Browser Support

extends is an ECMAScript6 (ES6) feature.

ES6 (JavaScript 2015) is supported in all current major browsers.

Chrome Edge Firefox Safari Opera
Yes Yes Yes Yes Yes

Internet Explorer 11 or older versions of IE do not support the extends keyword.


JavaScript 类(class)JavaScript Class

Other Extensions