JavaScript -klasverwysing
JavaScript-klasse
A class
is 'n tipe funksie, maar in plaas daarvan om die sleutelwoord
function
te gebruik om dit te begin, gebruik ons die sleutelwoord
class
, en die eienskappe word binne 'n
constructor()
metode toegeken:
Voorbeeld
Skep 'n Car-klas, en skep dan 'n voorwerp genaamd "mycar" gebaseer op die Car-klas:
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
Vir 'n tutoriaal oor klasse, lees ons JavaScript-klasse-tutoriaal .
Klasmetodes
Method | Description |
---|---|
constructor() | A special method for creating and initializing objects created within a class |
Klas sleutelwoorde
Keyword | Description |
---|---|
extends | Extends a class (inherit) |
static | Defines a static method for a class |
super | Refers to the parent class |