aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/operators/class/index.html
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/operators/class/index.html
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/operators/class/index.html')
-rw-r--r--files/zh-cn/web/javascript/reference/operators/class/index.html112
1 files changed, 112 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/operators/class/index.html b/files/zh-cn/web/javascript/reference/operators/class/index.html
new file mode 100644
index 0000000000..739c651513
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/operators/class/index.html
@@ -0,0 +1,112 @@
+---
+title: 类表达式
+slug: Web/JavaScript/Reference/Operators/class
+tags:
+ - Class
+ - Classes
+ - ES6
+translation_of: Web/JavaScript/Reference/Operators/class
+---
+<div>{{jsSidebar("Operators")}}</div>
+
+<p><strong>类表达式</strong>是用来定义类的一种语法。和<a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/function">函数表达式</a>相同的一点是,类表达式可以是命名也可以是匿名的。如果是命名类表达式,这个名字只能在类体内部才能访问到。JavaScript 的类也是基于原型继承的。</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox">const MyClass = class <em>[className]</em> [extends] {
+  // class body
+};</pre>
+
+<h2 id="描述">描述</h2>
+
+<p>类表达式的语法和<a href="/zh-CN/docs/Web/JavaScript/Reference/Statements/class">类语句</a>的语法很类似,只是在类表达式中,你可以省略掉类名,而类语句中不能。</p>
+
+<p>和类声明一样,类表达式中的代码也是强制<a href="/zh-CN/docs/Web/JavaScript/Reference/Strict_mode">严格模式</a>的。</p>
+
+<h2 id="示例">示例</h2>
+
+<h3 id="使用类表达式">使用类表达式</h3>
+
+<p>下面的代码使用类表达式语法创建了一个匿名类,然后赋值给变量 Foo。</p>
+
+<pre class="brush: js">let Foo = class {
+ constructor() {}
+ bar() {
+ return "Hello World!";
+ }
+};
+
+let instance = new Foo();
+instance.bar();
+// "Hello World!"
+</pre>
+
+<h3 id="命名类表达式">命名类表达式</h3>
+
+<p>如果你想在类体内部也能引用这个类本身,那么你就可以使用命名类表达式,并且这个类名只能在类体内部访问。</p>
+
+<pre class="brush: js">const Foo = class NamedFoo {
+ constructor() {}
+ whoIsThere() {
+ return NamedFoo.name;
+ }
+}
+
+let bar = new Foo();
+
+bar.whoIsThere();
+// "NamedFoo"
+
+NamedFoo.name;
+// ReferenceError: NamedFoo is not defined
+
+Foo.name;
+// "NamedFoo"
+
+</pre>
+
+<h2 id="规范">规范</h2>
+
+<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.operators.class")}}</p>
+
+<h2 id="相关链接">相关链接</h2>
+
+<ul>
+ <li><a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/function"><code>函数表达式</code></a></li>
+ <li><a href="/zh-CN/docs/Web/JavaScript/Reference/Statements/class"><code>类语句</code></a></li>
+ <li><a href="/zh-CN/docs/Web/JavaScript/Reference/Classes">类</a></li>
+</ul>