node class code example
Example 1: javascript create class
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
present = () => {
console.log(`Hi! i'm ${this.name} and i'm ${this.age} years old`)
}
}
let me = new Person("tsuy", 15);
me.present();
Example 2: javascript class
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
introduction() {
return `My name is ${name} and I am ${age} years old!`;
}
}
let john = new Person("John Smith", 18);
console.log(john.introduction());
Example 3: node js class
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
Rectangle.count++;
}
get area() {
return this.calcArea();
}
calcArea() {
return this.height * this.width;
}
static calcArea(width, height) {
return width * height;
}
}
Rectangle.count = 0;
const square = new Rectangle(10, 10);
console.log(square.height, square.width);
console.log(square.area);
console.log(square.calcArea());
console.log(Rectangle.count);
console.log(Rectangle.calcArea(15, 15));
Example 4: class declaration in javascript
class NameOfClass {
obj="text";
obj2="some other text";
}
console.log(new NameOfClass);