การอ้างอิงคลาส JavaScript
คลาส JavaScript
A class
เป็นประเภทของฟังก์ชัน แต่แทนที่จะใช้คีย์เวิร์ด
function
เพื่อเริ่มต้น เราใช้คีย์เวิร์ด
class
และคุณสมบัติถูกกำหนดไว้ใน
constructor()
เมธอด:
ตัวอย่าง
สร้างคลาสรถ แล้วสร้างวัตถุที่เรียกว่า "mycar" ตามคลาสรถ:
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
สำหรับบทช่วยสอนเกี่ยวกับคลาส โปรดอ่านJavaScript Classes Tutorialของเรา
วิธีการเรียน
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 |