Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Class Inheritance

JavaScript Class Inheritance

JavaScript class inheritance uses the extends keyword.

Inheritance allows us to define a class based on another class, which makes creating and maintaining an application easier.

The super() method is used to call the parent class's constructor.

When creating a class, you do not need to rewrite new data members and member functions; you only need to specify that the new class inherits the members of an existing class. This existing class is calledBase class (parent class), and the newly created class is calledDerived class (child class)。

Inheritance represents a is a relationship. For example, a mammal is an animal, a dog is a mammal, therefore a dog is an animal, and so on.

The code is as follows:

//

base class

class

Animal

{

//

eat() function

//

sleep() function

}

;

//

derived class

class

Dog

extends

Animal

{

//

bark() function

}

;

The class "Runoob" created in the following example inherits from the "Site" class:

Example

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 »

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.

Inheritance is useful for code reusability.

JavaScript does not have traditional classes like other programming languages; instead, it is based on a prototype inheritance model.

ES6 introduced classes and the class keyword, but the underlying mechanism is still based on prototype inheritance.

Using Prototype Chain Inheritance

In the following example, Animal is a base class and Dog is a child class that inherits from Animal. Dog.prototype uses Object.create(Animal.prototype) to create a new object that inherits the methods and properties of Animal.prototype. By setting Dog.prototype.constructor to Dog, we ensure that the constructor in the inheritance chain is correct.

Example

function

Animal

(

name

)

{

this

.

name

=

name

;

}

Animal

.

prototype

.

eat

=

function

(

)

{

console

.

log

(

this

.

name

"

is eating.

"

)

;

}

;

function

Dog

(

name

,

breed

)

{

Animal

.

call

(

this

,

name

)

;

this

.

breed

=

breed

;

}

//

Establishes the prototype chain so that Dog inherits from Animal

Dog

.

prototype

=

Object

.

create

(

Animal

.

prototype

)

;

Dog

.

prototype

.

constructor

=

Dog

;

Dog

.

prototype

.

bark

=

function

(

)

{

console

.

log

(

this

.

name

"

is barking.

"

)

;

}

;

var

dog

=

new

Dog

(

"

Buddy

"

,

"

Labrador

"

)

;

dog

.

eat

(

)

;

//

Calls the method inherited from Animal

dog

.

bark

(

)

;

//

Calls the method of Dog

Using ES6 Class Inheritance

ES6 introduced the class keyword, making class definition and inheritance clearer; the extends keyword is used to establish an inheritance relationship, and the super keyword is used in a child class constructor to call the parent class's constructor.

Example

class

Animal

{

constructor

(

name

)

{

this

.

name

=

name

;

}

eat

(

)

{

console

.

log

(

this

.

name

"

is eating.

"

)

;

}

}

class

Dog

extends

Animal

{

constructor

(

name

,

breed

)

{

super

(

name

)

;

this

.

breed

=

breed

;

}

bark

(

)

{

console

.

log

(

this

.

name

"

is barking.

"

)

;

}

}

const

dog

=

new

Dog

(

"

Buddy

"

,

"

Labrador

"

)

;

dog

.

eat

(

)

;

dog

.

bark

(

)

;

Whether using prototype chain inheritance or ES6 class inheritance, similar inheritance results can be achieved; the choice can be made according to personal preference and project requirements.


getter and setter

In a class, we can use getter and setter to get and set values; both getter and setter must execute in strict mode.

getter and setter can make our operations on properties very flexible.

To add getter and setter in a class, use the get and set keywords.

The following example creates a getter and setter for the sitename property:

Example

class

Runoob

{

constructor

(

name

)

{

this

.

sitename

=

name

;

}

get

s_name

(

)

{

return

this

.

sitename

;

}

set

s_name

(

x

)

{

this

.

sitename

=

x

;

}

}

let

noob

=

new

Runoob

(

"

Runoob Tutorial

"

)

;

document

.

getElementById

(

"

demo

"

)

.

innerHTML

=

noob

.

s_name

;

Try it »

**Note:** Even though a getter is a method, do not use parentheses when you want to get the property value.

The name of a getter/setter method cannot be the same as the name of the property; in this example the property name is sitename.

Many developers use an underscore character _ before the property name to separate the getter/setter from the actual property:

The following example uses an underscore _ for the property and creates the corresponding getter/setter methods:

Example

class

Runoob

{

constructor

(

name

)

{

this

.

_sitename

=

name

;

}

get

sitename

(

)

{

return

this

.

_sitename

;

}

set

sitename

(

x

)

{

this

.

_sitename

=

x

;

}

}

let

noob

=

new

Runoob

(

"

Runoob Tutorial

"

)

;

document

.

getElementById

(

"

demo

"

)

.

innerHTML

=

noob

.

sitename

;

Try it »

To use a setter, use the same syntax as when setting a property value; although set is a method, it must be used withoutBracket:

Example

class

Runoob

{

constructor

(

name

)

{

this

.

_sitename

=

name

;

}

set

sitename

(

x

)

{

this

.

_sitename

=

x

;

}

get

sitename

(

)

{

return

this

.

_sitename

;

}

}

let

noob

=

new

Runoob

(

"

Runoob Tutorial

"

)

;

noob

.

sitename

=

"

RUNOOB

"

;

document

.

getElementById

(

"

demo

"

)

.

innerHTML

=

noob

.

sitename

;

Try it »

Hoisting

An important difference between function declarations and class declarations is that function declarations are hoisted, while class declarations are not.

You must first declare your class and then access it; otherwise code like the following will throw a ReferenceError:

Example

//

The class cannot be used here because it has not been declared yet

//

noob = new Runoob("Runoob Tutorial")

//

Error

class

Runoob

{

constructor

(

name

)

{

this

.

sitename

=

name

;

}

}

//

The class can be used here

let

noob

=

new

Runoob

(

"

Runoob Tutorial

"

)

Using a class before it is declared raises an error:

Declaring it before use allows normal execution: