1 Jawaban
24 Dilihat
1 tahun yang lalu

Halo kak, saya Fadhil Rahman dari kelas ReactJS & NodeJS - New ingin bertanya materi Basic OOP.

apakah oop javascript sering di pakai di dunia kerja?


ReactJS & NodeJS - New Basic OOP
1 Jawaban
Sort

tentu saja sangat penting, karena membantu membuat kode yang lebih bersih, terstruktur, dan mudah dipahami sendiri maupun oranglain.


Contoh OOP menggunakan kelas:

class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }
  startEngine() {
    return `The engine of ${this.make} ${this.model} ${this.year} is started.`;
  }
  drive() {
    return `${this.make} ${this.model} ${this.year} is now being driven.`;
  }
  stop() {
    return `${this.make} ${this.model} ${this.year} has stopped.`;
  }
}
const myCar = new Car("Toyota", "Camry", 2020);
console.log(myCar.startEngine());
console.log(myCar.drive());
console.log(myCar.stop());

Contoh OOP bawaan JS:
let car = {
  make: "Toyota",
  model: "Camry",
  year: 2020,
  startEngine: function() {
    return `The engine of ${this.make} ${this.model} ${this.year} is started.`;
  },
  drive: function() {
    return `${this.make} ${this.model} ${this.year} is now being driven.`;
  },
  stop: function() {
    return `${this.make} ${this.model} ${this.year} has stopped.`;
  }
};

console.log(car.startEngine());
console.log(car.drive());
console.log(car.stop());

1
avatar
Prof. Reza
Di Jawab
1 tahun yang lalu
Di Update
1 tahun yang lalu
Newest Question