xxxxxxxxxx
class Point {
constructor(x, y) {
this.x = x
this.y = y
}
toString() {
return '[X=' + this.x + ', Y=' + this.y + ']'
}
}
class ColorPoint extends Point {
static default() {
return new ColorPoint(0, 0, 'black')
}
constructor(x, y, color) {
super(x, y)
this.color = color
}
toString() {
return '[X=' + this.x + ', Y=' + this.y + ', color=' + this.color + ']'
}
}
console.log('The first point is ' + new Point(2, 10))
console.log('The second point is ' + new ColorPoint(2, 10, 'green'))
console.log('The default color point is ' + ColorPoint.default())
xxxxxxxxxx
class MyClass {
constructor() {
this.answer = 42;
}
}
const obj = new MyClass();
obj.answer; // 42
xxxxxxxxxx
class Person {
constructor(name) {
this.name = name;
}
greet() {
console.log(`Hello, ${this.name}!`);
}
}
const john = new Person('John');
john.greet(); // Output: Hello, John!
xxxxxxxxxx
var Polygon = class {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
xxxxxxxxxx
class Polygon {
constructor(sides) {
this.sides = sides;
}
// Method
*getSides() {
for(const side of this.sides){
yield side;
}
}
}
const pentagon = new Polygon(1,2,3,4,5);
console.log([pentagon.getSides()]); // [1,2,3,4,5]