Referensi Kelas JavaScript
Kelas JavaScript
A class
adalah jenis fungsi, tetapi alih-alih menggunakan kata kunci
function
untuk memulainya, kami menggunakan kata kunci
class
, dan properti ditetapkan di dalam
constructor()
metode:
Contoh
Buat kelas Mobil, lalu buat objek bernama "mycar" berdasarkan kelas Mobil:
class Car { // Create a class
constructor(brand) { // Class constructor
this.carname = brand;
// Class body/properties
}
}
mycar = new Car("Ford"); // Create an object of Car
class
Untuk tutorial tentang kelas, baca Tutorial Kelas JavaScript kami .
Metode Kelas
Method | Description |
---|---|
constructor() | A special method for creating and initializing objects created within a class |
Kata Kunci Kelas
Keyword | Description |
---|---|
extends | Extends a class (inherit) |
static | Defines a static method for a class |
super | Refers to the parent class |