JavaScript Class extends Keyword
Example
Example
The class "Runoob" created in the following example inherits from the "Site" class:
class
Site
{
constructor
(
name
)
{
this
.
sitename
=
name
;
}
present
(
)
{
return
'
I like
'
this
.
sitename
;
}
}
class
Runoob
extends
Site
{
constructor
(
name
,
age
)
{
super
(
name
)
;
this
.
age
=
age
;
}
show
(
)
{
return
this
.
present
(
)
'
, it created
'
this
.
age
'
years.
'
;
}
}
let
noob
=
new
Runoob
(
"
Runoob Tutorial
"
,
5
)
;
document
.
getElementById
(
"
demo
"
)
.
innerHTML
=
noob
.
show
(
)
;
Try it »
Definition and Usage
The extends keyword is used to create a class that is a subclass of another class.
The subclass inherits all the methods of the other class.
Inheritance is useful for code reusability: when creating a new class, reuse the properties and methods of an existing class.
The super() method refers to the parent class's constructor.
By calling the super() method inside the constructor, we call the parent class's constructor, so that 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 by all major browsers today.
| Chrome | Edge | Firefox | Safari | Opera |
| Yes | Yes | Yes | Yes | Yes |
Internet Explorer 11 or older IE does not support the extends keyword.
JavaScript Classes (class)