> For the complete documentation index, see [llms.txt](https://anjia1.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://anjia1.gitbook.io/web/js/data-type/object/class.md).

# Class

JavaScript 中的 class 是建立在 prototype 之上的，但它也有一些 class 特有的 syntax 和 semantics。

class 实际上是一种特殊的 function，它也有两种定义方式：

1. class declaration 类声明
   * 但是，它不会被提升，也有 temporal dead zone（临时死区）限制，同 `let`, `const`
2. class expression 类表达式

```js
// Declaration
class Rectangle {
    constructor(height, width) {
        this.height = height
        this.width = width
    }
}

// Expression: the class is anonymous but assigned to a variable
const Rectangle = class {
    constructor(height, width) {
        this.height = height
        this.width = width
    }
};

// Expression: the class has its own name
const Rectangle = class Rectangle2 {
    constructor(height, width) {
        this.height = height
        this.width = width
    }
}
```

class 的代码体默认运行在 strict mode。

class 元素可以通过以下三个方面来表征：

| Kind                                          | Location                  | Visibility               |
| --------------------------------------------- | ------------------------- | ------------------------ |
| <p>getter<br>setter<br>method</p><p>field</p> | <p>instance<br>static</p> | <p>public<br>private</p> |

## `constructor()`

`constructor()` 用来 create 和 initialize 一个对象。在一个 class 中，只能有一个 `constructor()`。

有两种字段：instance properties 和 class fields。

如果 instance properties 的 values 不依赖 constructor 的 arguments，则可以用 class fields。class fields 类似于 object properties 而不是 variables，所以声明时不需要使用 `const` 等关键字。如果声明时没有 default value，则默认是 `undefined`。

```javascript
class Rectangle {
    // class fields
    height = 0
    width;

    constructor(height, width) {
        // instance properties
        this.height = height
        this.width = width
    }
}
```

```javascript
class Rectangle {
    // private fields
    #height = 0
    #width
    constructor(height, width) {
        this.#height = height
        this.#width = width
    }
}
```

说明：private 特性有一个限制，那就是在同一个 class 中声明的所有 property names 必须唯一。而 public properties 则没有这个限制，当有多个 public property names 时，最后一个会 overwrites 其它的。

## static initialization blocks

## methods

可以是：

* plain function
* async function
* generator function
* async generator function

## 继承 inheritance


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://anjia1.gitbook.io/web/js/data-type/object/class.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.
