class

Class declaration

1
2
3
4
5
6
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
}

A class expression is another way to define a class. Class expressions can be named or unnamed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// unnamed
let Rectangle = class {
constructor(height, width) {
this.height = height;
this.width = width;
}
};
console.log(Rectangle.name);
// output: "Rectangle"

// named
let Rectangle = class Rectangle2 {
constructor(height, width) {
this.height = height;
this.width = width;
}
};
console.log(Rectangle.name);
// output: "Rectangle2"

Understand difference between [class] and function constructor

An important difference between function declarations and class declarations is that function declarations are hoisted and class declarations are not. You first need to declare your class and then access it, otherwise code like the following will throw a ReferenceError:

1
2
3
const p = new Rectangle(); // ReferenceError

class Rectangle {}

Understand difference between [class] and function constructor

The constructor method is a special method for creating and initializing an object created with a class. There can only be one special method with the name “constructor” in a class. A SyntaxError will be thrown if the class contains more than one occurrence of a constructor method.

A constructor can use the super keyword to call the constructor of the super class.

Understand difference between method and [class] method

Prototype methods

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
// Getter
get area() {
return this.calcArea();
}
// Method
calcArea() {
return this.height * this.width;
}
}

const square = new Rectangle(10, 10);

console.log(square.area); // 100

Static methods

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}

static distance(a, b) {
const dx = a.x - b.x;
const dy = a.y - b.y;

return Math.hypot(dx, dy);
}
}

const p1 = new Point(5, 5);
const p2 = new Point(10, 10);

console.log(Point.distance(p1, p2)); // 7.0710678118654755

Inheritance

The extends keyword is used in class declarations or class expressions to create a class as a child of another class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Animal { 
constructor(name) {
this.name = name;
}

speak() {
console.log(`${this.name} makes a noise.`);
}
}

class Dog extends Animal {
constructor(name) {
super(name); // call the super class constructor and pass in the name parameter
}

speak() {
console.log(`${this.name} barks.`);
}
}

let d = new Dog('Mitzie');
d.speak(); // Mitzie barks.

Getter/setter

The get syntax binds an object property to a function that will be called when that property is looked up.

1
2
3
4
5
6
7
8
9
10
11
12
var obj = {
log: ['a', 'b', 'c'],
get latest() {
if (this.log.length == 0) {
return undefined;
}
return this.log[this.log.length - 1];
}
}

console.log(obj.latest);
// expected output: "c"

The set syntax binds an object property to a function to be called when there is an attempt to set that property.

1
2
3
4
5
6
7
8
9
10
11
12
var language = {
set current(name) {
this.log.push(name);
},
log: []
}

language.current = 'EN';
language.current = 'FA';

console.log(language.log);
// expected output: Array ["EN", "FA"]