aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/statements/class/index.html
blob: b4b8afdbd6d62c0d5ce7d9f624e489c04928bf24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
---
title: class
slug: Web/JavaScript/Reference/Statements/class
tags:
  - Class
  - Classes
  - Declaration
  - ECMAScript 2015
  - ES6
  - JavaScript
  - Reference
  - Statement
translation_of: Web/JavaScript/Reference/Statements/class
---
<div>{{jsSidebar("Statements")}}</div>

<p><strong>class 声明</strong>创建一个基于原型继承的具有给定名称的新类。</p>

<p>{{EmbedInteractiveExample("pages/js/statement-class.html")}}</p>

<div class="noinclude">
<p>你也可以使用{{jsxref("Operators/class", "类表达式", "", 1)}}定义类。但是不同于类表达式,类声明不允许再次声明已经存在的类,否则将会抛出一个类型错误。</p>
</div>

<h2 id="语法">语法</h2>

<pre class="brush: js">class <em>name</em> [extends] {
  // class body
}
</pre>

<h2 id="描述">描述</h2>

<p>和类表达式一样,类声明体在<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Strict_mode">严格模式</a>下运行。构造函数是可选的。</p>

<p>类声明不可以提升(这与<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/function">函数声明</a>不同)。</p>

<h2 id="示例">示例</h2>

<h3 id="声明一个类">声明一个类</h3>

<p>在下面的例子中,我们首先定义一个名为Polygon的类,然后继承它来创建一个名为Square的类。注意,构造函数中使用的 super() 只能在构造函数中使用,并且必须在使用 this 关键字前调用。</p>

<pre class="brush: js">class Polygon {
  constructor(height, width) {
    this.name = 'Polygon';
    this.height = height;
    this.width = width;
  }
}

class Square extends Polygon {
  constructor(length) {
    super(length, length);
    this.name = 'Square';
  }
}</pre>

<div class="warning">
<h3 id="重复定义类">重复定义类</h3>

<p>重复声明一个类会引起类型错误。</p>

<pre class="brush: js">class Foo {};
class Foo {};
// Uncaught TypeError: Identifier 'Foo' has already been declared</pre>

<p>若之前使用类表达式定义了一个类,则再次声明这个类同样会引起类型错误。</p>

<pre class="brush: js">let Foo = class {};
class Foo {};
// Uncaught TypeError: Identifier 'Foo' has already been declared</pre>
</div>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES2015', '#sec-class-definitions', 'Class definitions')}}</td>
   <td>{{Spec2('ES2015')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES2016', '#sec-class-definitions', 'Class definitions')}}</td>
   <td>{{Spec2('ES2016')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES2017', '#sec-class-definitions', 'Class definitions')}}</td>
   <td>{{Spec2('ES2017')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-class-definitions', 'Class definitions')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容">浏览器兼容</h2>

<p>{{Compat("javascript.statements.class")}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/function"><code>function</code> declaration</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/class"><code>class</code> expression</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Classes">Classes</a></li>
</ul>