Class - Tuong-Nguyen/JavaScript-Structure GitHub Wiki

Declaration

class Drone{

}

let drone = new Drone();
expect(drone instanceof Drone).toBeTruthy();

Constructor

class Drone{
  constructor(id, name){
    this.id = id;
    this.name = name;
  }
}

let drone = new Drone('A123', 'Drone');
Console.log(drone.id, drone.name);
Console.log(drone['id'], drone['name']);

Method

fly(){
 return `Drone ${this.id} is flying`;
}

Property

Getter and Setter

class Drone{
  constructor(id, name){
    this._id = id;
    this.name = name;
  }
  
  get id(){
    return this._id;
  }
  
  set id(value){
    this._id = value;
  }
}
  • _id is considered as private.
  • id property has getter and setter.

Static method

static getCompany(){
    return 'ABC company';
}

Static property

export default class Drone{
  constructor(id, name){
    this.id = id;
    this.name = name;
  }
}
// Static property
Drone.maxHeight = 100;