Day 4 : Classes
Theory
// 1. Class Declaration
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
let p = new Polygon(1, 2);
console.log('Polygon p:', p);
// 2. Class Expression ( can be named or unnamed)
let Polygon = class {
constructor(height, width) {
this.height = height;
this.width = width;
}
};
// Named expression
let Polygon = class Polygon { // the name is right after " class" keyword
constructor(height, width) {
this.height = height;
this.width = width;
}
};
// 3. Constructors
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
getArea() {
return this.height * this.width;
}
}
const square = new Polygon(10, 10);
console.log(square.getArea());Task
We also learnt to use .reduce method
Last updated