Deklarasi class digunakan untuk membuat class baru dengan nama menggunakan turunan berbasis prototype.
Anda juga bisa mendefinisikan kelas menggunakanclass expression.
Sintaks
class name [extends] {
// class body
}
Deskripsi
Seperti halnya dengan ekspresi class, pembentuk (body) class dari deklarasi class di jalankan pada strict mode.
Deklarasi class bukanlah hoisted (Tidak seperti function declarations).
Contoh
Dasar deklarasi class
Pada contoh, hal pertama yang kita lakukan adalah mendefinisikan class dengan nama Polygon, kemudian membentangkannya ( extend ) untuk membuat class bernama Square. Perlu diperhatikan bahwa super(), digunakan pada constructor, hanya dapat digunakan di konstruktor dan harus dipanggil setelah keyword 'this' dapat digunakan.
class Polygon {
constructor(height, width) {
this.name = 'Polygon';
this.height = height;
this.width = width;
}
}
class Square extends Polygon {
constructor(length) {
super(length, length);
this.name = 'Square';
}
}
Spesifikasi
Spesifikasi | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Class definitions' in that specification. |
Standard | Initial definition. |
ECMAScript (ECMA-262) The definition of 'Class definitions' in that specification. |
Living Standard |
Kompabilitas Browser
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Fitur | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Dukungan dasar | 42.0 | 45 (45) | ? | ? | ? |
Array subclassing | 43.0 | No support | ? | ? | ? |
Allowed in sloppy mode | 49.0 |
Fitur | Android | Android Webview | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
Dukungan dasar | No support | 42.0 | 45.0 (45) | ? | ? | ? | 42.0 |
Array subclassing | No support | 43.0 | No support | ? | ? | ? | 43.0 |
Allowed in sloppy mode | No support | 49.0 | 49.0 |