Dalam paradigma Object-Oriented Programming (OOP), class merupakan sebuah blueprint yang dapat dikembangkan untuk membuat sebuah objek.
Blueprint ini merupakan sebuah template yang di dalamnya menjelaskan seperti apa perilaku dari objek itu (berupa properti ataupun method).
2020031400163071868d05b7fff5009d785e8cb61890d3.png
Paradigma OOP selalu digambarkan dengan kehidupan nyata. Visualisasi di atas mencontohkan gambaran umum OOP di mana terdapat sebuah blueprint kucing, nilai yang dimiliki kucing, dan kemampuan yang dapat dilakukan olehnya. 
Dalam OOP blueprint tersebut dikenal dengan class (kelas), nilai yang dimiliki olehnya dikenal dengan properti, kemampuan yang dimilikinya dikenal sebagai behaviour/method dan realisasi dari sebuah blueprint tersebut disebut instance.
Dalam dunia pemrograman khususnya dalam paradigma OOP, kita sering membuat banyak objek dengan jenis yang sama. Dengan membuat blueprint ini kita dapat mengurangi duplikasi kode dalam membuat objek yang serupa
Kebanyakan bahasa pemrograman memanfaatkan class dalam penerapan paradigma OOP. Pada JavaScript tidak ada konsep class murni bahkan hingga saat ini. Namun bukan berarti kita tidak bisa menerapkan paradigma OOP sepenuhnya.
Seperti yang kita ketahui, class pada OOP merupakan sebuah blueprint. Jika berbicara mengenai blueprint, JavaScript memiliki konsep untuk membuatnya tanpa melalui class. Konsep tersebut adalah prototype. Sejak awal developer menggunakan konsep ini dalam menerapkan paradigma OOP di JavaScript.
Walaupun dapat menggantikan class, konsep prototype tidak serupa dengan class pada bahasa lain. Terlebih pada penulisan sintaknya. Hal ini menjadikan banyak developer kebingungan khususnya developer yang dasarnya dari bahasa class basis (seperti Java, C++, C#, Swift, etc.). Meskipun banyak juga developer yang merasa bahwa class tidak diperlukan pada JavaScript, namun fitur class ini tetap menjadi salah satu yang dikembangkan pada ECMAScript 6.

A Class Before ES6

Sebelum ES6, Hal yang paling mendekati dengan class yaitu membuat sebuah objek menggunakan constructor function dan keyword new, lalu untuk menambahkan method kita gunakan konsep prototype.

  1. function Car(manufacture, color) {

  2.     this.manufacture = manufacture;

  3.     this.color = color;

  4.     this.enginesActive = false;

  5. }

  6.  

  7. Car.prototype.startEngines = function () {

  8.     console.log('Mobil dinyalakan...');

  9.     this.enginesActive = true;

  10. };

  11.  

  12. Car.prototype.info = function () {

  13.     console.log("Manufacture: " + this.manufacture);

  14.     console.log("Color: " + this.color);

  15.     console.log("Engines: " + (this.enginesActive ? "Active" : "Inactive"));

  16. }

  17.  

  18. var johnCar = new Car("Honda", "Red");

  19. johnCar.startEngines();

  20. johnCar.info();

  21.  

  22. /* output: 

  23. Mobil dinyalakan...

  24. Manufacture: Honda

  25. Color: Red

  26. Engines: Active

  27. */


Pada kode di atas Car merupakan constructor function yang akan membuat instance Car baru setiapkan kode new Car() dieksekusi. Melalui Car.prototype, method startEngines() dan carInfo()  diwarisi pada setiap instance Car yang dibuat, sehingga johnCar (sebagai instance Car) dapat mengakses kedua method tersebut.
Teknik dasar ini yang digunakan dalam membuat class di JavaScript sebelum ES6.
“Mengapa method pada instance harus disimpan pada prototype atau __proto__ ? Mengapa tidak disimpan pada constructor sama seperti properti? 
Alasannya adalah jika kita menyimpan method pada constructor maka method tersebut akan selalu dibuat ketika instance dibuat. Ini bukan pendekatan yang baik karena jika method memiliki kode yang banyak, maka akan memakan memori yang banyak.
Sedangkan jika menggunakan prototype, method hanya dibuat satu kali. Dan method tersebut diwarisi kepada setiap instance yang dibuat.”

ES6 Classes

Dengan hadirnya class pada ES6, pembuatan class di JavaScript menjadi lebih mudah dan juga penulisannya mirip seperti bahasa pemrograman lain berbasis class. Pembuatan class pada ES6 menggunakan keyword class itu sendiri kemudian diikuti dengan nama class-nya. 

  1. class Car {

  2.     

  3.     // Sama seperti function constructor

  4.     constructor(manufacture, color) {

  5.         this.manufacture = manufacture;

  6.         this.color = color;

  7.         this.enginesActive = false;

  8.     }

  9.     

  10.     // Sama seperti Car.prototype.startEngine

  11.     startEngines() {

  12.         console.log('Mobil dinyalakan...');

  13.         this.enginesActive = true;

  14.     }

  15.     

  16.     // Sama seperti car.prototype.info

  17.     info() {

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

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

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

  21.     }

  22.     

  23. }

  24.  

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

  26. johnCar.startEngines();

  27. johnCar.info();

  28.  

  29.  

  30. /* output: 

  31. Mobil dinyalakan...

  32. Manufacture: Honda

  33. Color: Red

  34. Engines: Active

  35. */


Jika Anda terbiasa dengan bahasa pemrograman berbasis class, pasti penulisannya sangat serupa bukan?

Walaupun dari segi sintaksis pembuatan class antara keduanya cukup berbeda, namun perilaku dari objek yang dibuat dengan keduanya sama.

Inilah mengapa class pada ES6 hanya sebuah syntactic sugar dari konsep prototype yang sudah ada.
“Ketika kita hendak membuat sebuah constructor function ataupun class. Secara code convention (aturan penulisan), gunakan CamelCase dalam penamaannya. Contohnya Car daripada car, SportCar daripada sportCar atau Sportcar
Apa Itu Class di Javascript?
Bookmark

Apa Itu Class di Javascript?

Type:
Genre:
Status:
Country:
Keywords: Watch online Apa Itu Class di Javascript? bahasa Indonesia, Streaming anime Apa Itu Class di Javascript? sub indo, Mau nonton anime terbaik dengan subtitle Indonesia? Streaming anime Jepang dari anime Apa Itu Class di Javascript?, website streaming anime subtitle indonesia dan nonton anime indo update setiap hari, TV Apa Itu Class di Javascript?, Terbaru Terupdate dan Terlengkap dengan Kualitas 1080p, 720p. 480p, 360p Anime Sub Indo.

Comments

Comment Report Rule
Show Comments
9Anime Comment Policies & Guidelines
  1. Spoiling Content

    Definition

    “Anime Comment” is a spoiler-free section and is usually used for a general discussion about anime without spoiling any content.

    “Episode Comment” may contain spoilers for current or previous episodes. Contents from manga or future episodes are considered as spoilers.

    Hiding spoilers with spoiler tags is not allowed either.

    Consequences

    Minor Spoiler:-

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

    Major Spoiler:-

    1st Offense: Permanent Ban

  2. Diversion From Topic

    Definition

    The comment section should be about the anime on hand. Although references from other anime may be used, the core topic of the comments should be about the anime at hand.

    This rule also applies on advertising other streaming websites which results in an instant ban.

    This rule also applies to spam, posting comments like “first”, “second”, etc. are considered spam.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  3. Flame War

    Definition

    Exchange of angry or abusive messages between the comments. Any form of insulting is also considered as flame war.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  4. Posting Harmful Link

    Definition

    Post of hyperlinks for websites that may contain viruses or may harm your PC or device.

    Consequences

    1st Offense: Permanent Ban + Deletion of All Comments

  5. Posting NSFW Content

    Definition

    Posting contents that are not safe for work, such as pornographic images or video, ecchi anime, hentai, etc.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  6. Inappropriate Profile Picture

    Definition

    Using a profile picture that is not safe for work. Although minor ecchi or fanservice is acceptable.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  7. Use of Common Sense

    Definition

    If you think that you’ll get in trouble for what you’re about to do, don’t do it.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  8. Upvote Abusing

    Definition

    Creating multiple alternative accounts to upvote your own comments.

    Consequences

    1st Offense: Permanent Ban on All Accounts + Deletion of All Comments

  9. Downvote Abusing

    Definition

    Targeting a user and downvoting all his comments, or creating multiple accounts to downvote one or more comments.

    Consequences

    1st Offense: Permanent Ban on All Accounts

  10. Swearing

    Definition

    Swearing is fine, as long as it’s not directed towards anyone, even as a joke. It may be misinterpreted by a moderator.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  11. Moderator Verdict

    Definition

    A moderator’s verdict is final and arguing with them will only cause further punishment. If you have a complaint against a staff member, or wish to file a ban appeal, go to the 9Anime Discord Server and post your issue in the #disqus-appeals channel.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

Think you have been unfairly warned or banned?

If you believe that you’ve been unfairly banned or if you have a complaint against one or more Disqus Staff Members, you may post an appeal or complaint on 9Anime Discord Server.

STEPS

  1. Join 9Anime Discord Server: https://discord.gg/xxxxxxx
  2. Read the #rules channel and follow instructions. (Optional, but highly recommended)
  3. Head towards #disqus-appeals channel.
  4. Use the appropriate template to appeal a ban or file a complaint.