JSFiddle - React, Tailwind, and code Playground
by vinay68
JavaScript
//Static methods in javascript
class car{
constructor(color,price){
Object.assign(this, {color,price});
}
static comparePrice(car1, car2){
//console.dir(this);
return Math.abs(car1.price - car2.price);
}
getColor(){
//console.dir(this);
return this.color;
}
}
var redCar = new car('red', 100);
var blueCar = new car('blue', 110);
console.log(redCar.getColor());
console.log(car.comparePrice(redCar, blueCar));
//Another example
class newCar{
constructor(price){
return this.price = price;
}
static sellcar(newCar1){
return `selling for ${this.price}`;
}
}
class Toyota extends newCar{
constructor(price){
super(price);
}
static sellcar(newCar1){
return `Toyota ${super.sellCar(newCar1)}`;
}
}
const camry = new Toyota();
console.log(Toyota.sellcar(camry));