Cara Menambahkan Method didalam Class Javascript - Method Class Javascript

Cara Menambahkan Method didalam Class Javascript - Method Class Javascript

guna menambahkan method pada class, kita juga cukup menuliskannya pada body class, serta tidak perlu melalui prototype seperti menggunakan constructor function.

  1. class Car {

  2.     constructor(manufacture, color) {

  3.         this.manufacture = manufacture;

  4.         this.color = color;

  5.         this.enginesActive = false;

  6.     }

  7.     

  8.     startEngines() {

  9.         console.log("Mesin dinyalakan");

  10.         this.enginesActive = true;

  11.     }

  12.     

  13.     info() {

  14.         console.log(`Manufacture: ${this.manufacture}`);

  15.         console.log(`Color: ${this.color}`);

  16.         console.log(`Engines: ${this.manufacture ? "Active" : "Inactive"}`);

  17.     }

  18. }

  19.  

  20. const johnCar = new Car("Honda", "Red");

  21.  

  22. johnCar.startEngines();

  23. johnCar.info();

  24.  

  25.  

  26. /* output:

  27. Mesin dinyalakan

  28. Manufacture: Honda

  29. Color: Red

  30. Engines: Active

  31. */


Dengan menggunakan class, meskipun kita menuliskan method pada body class, akan tetapi method tersebut tetap berada pada prototype chain miliki instance yang terbuat. Kita juga bisa melihat bagaimana objek yang dibuat menggunakan class pada console browser
20200312163128914b6b721efd67a21c9e124a0111699e.gif

About the Author

Rudi Kilam merupakan seorang pelajar yang mempunyai keinginan dan memiliki minat menulis sebuah artikel terkait dengan pengetahuan umum, IT, kesehatan, ekonomi, sosiologi, pembudidayaan dan lain sebagainya.
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.