declare variable javascript class code example

Example 1: javascript class

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

Example 2: javascript create class

class Car {
  constructor(brand) {
    this.carname = brand;
  }
}

Example 3: javascript classes

let Person = class {
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
}

Example 4: variables in js class

constructor(){
    this.foo = bar
}

Tags:

Java Example