# 02. Class Declaration

Синтаксис для классов выглядит так:

```javascript
class Название [extends Родитель]  {
  constructor() { ... }
  методы
  static методы
}
```

Определения классов:

* Класс нельзя вызывать без `new`, будет ошибка.
* Объявление класса с точки зрения области видимости ведёт себя как `let`.
* Имеют блочную область видимости
* Не формируют свойство глобального объекта.
* Не поднимаются.
* Класс-родитель должен быть объявлен выше.
* `[extends Родитель]` - может быть выражением, результатом которого должен быть класс или функция-конструктор.
* Тело класса может содержать только методы, но не данные.

Причина того, что объявления классов не поднимаются заключается в том, что связь `extends` может содержать выражения, которые должны выполняться в соответствующее время.

```javascript
const identity = x => x;

// Here we are in the temporal dead zone of `MyClass`
let inst = new MyClass(); // ReferenceError

// Note the expression in the `extends` clause
class MyClass extends identity(Object) {}
```

Ограничение на подъем классов не такое радикальное. Можно ссылаться на класс до его объявления, но этот не должен выполняться:

```javascript
function functionThatUsesBar() {
  return new Bar();
}

functionThatUsesBar(); // ReferenceError
class Bar {}
functionThatUsesBar(); // OK
```

## Class expressions

Так же, как и *Function Expression*, классы можно задавать, в любом выражении и внутри вызова функции. Это называется *Class Expression*. У класса может быть опциональное имя, доступное только внутри класса:

```javascript
const MyClass = class Me {
  getClassName() {
    return Me.name;
  }
};

let inst = new MyClass();
console.log(inst.getClassName()); // Me
console.log(Me.name); // ReferenceError: Me is not defined
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://strctr.gitbook.io/programming/01-languages/javascript/01-language/c-functions/c3-classes/02-class-definition.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
