Оновлено: 19.05.2025

Classes

Класи JavaScript

Class— це тип функції, але замість використання ключового слова functionдля її ініціювання ми використовуємо ключове слово class, а властивості призначаються всередині constructor()методу:

 

 

приклад

Створіть клас Car, а потім створіть об’єкт під назвою «mycar» на основі класу Car:
 

 

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

 

Методи класу

 

constructor()constructor()

A special method for creating and initializing objects created within a class

 

Ключові слова класу

 

extendsextends

Extends a class (inherit)

staticstatic

Defines a static method for a class

supersuper

Refers to the parent class

Object