A class
is a type of function, but instead of using the keyword function
to initiate it, we use the keyword class
, and the properties are assigned inside a constructor()
method:
Create a Car class, and then create an object called "mycar" based on the Car class:
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
Method | Description |
---|---|
constructor() | A special method for creating and initializing objects created within a class |
Keyword | Description |
---|---|
extends | Extends a class (inherit) |
static | Defines a static method for a class |
super | Refers to the parent class |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!