class

Defination

The class declaration creates a new class with a given name using prototype-based inheritance.

Syntax

1
class name [extends] {
2
// class body
3
}
Copied!

Examples

1
class Polygon {
2
constructor(height, width) {
3
this.name = 'Polygon';
4
this.height = height;
5
this.width = width;
6
}
7
}
8
9
class Square extends Polygon {
10
constructor(length) {
11
super(length, length);
12
this.name = 'Square';
13
}
14
}
Copied!

Usage

Example from react.
1
class Clock extends React.Component {
2
constructor(props) {
3
super(props);
4
this.state = {date: new Date()};
5
}
6
7
render() {
8
return (
9
<div>
10
<h1>Hello, world!</h1>
11
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
12
</div>
13
);
14
}
15
}
Copied!

References