js function class code example
Example 1: javascript class
class ClassMates{
constructor(name,age){
this.name=name;
this.age=age;
}
displayInfo(){
return this.name + "is " + this.age + " years old!";
}
}
let classmate = new ClassMates("Mike Will",15);
classmate.displayInfo();
Example 2: 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 3: javascript 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: javascript class
let Rectangle = class {
constructor(height, width) {
this.height = height;
this.width = width;
}
};
console.log(Rectangle.name);
let Rectangle = class Rectangle5 {
constructor(height, width) {
this.height = height;
this.width = width;
}
};
console.log(Rectangle.name);
Example 5: js class
class ClassName {
constructor() { ... }
method_1() { ... }
method_2() { ... }
method_3() { ... }
}
Example 6: how to create class in javascript
class ClassName
{
}