aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/classes/extends
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/javascript/reference/classes/extends')
-rw-r--r--files/zh-cn/web/javascript/reference/classes/extends/index.html106
1 files changed, 106 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/classes/extends/index.html b/files/zh-cn/web/javascript/reference/classes/extends/index.html
new file mode 100644
index 0000000000..3166b658fc
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/classes/extends/index.html
@@ -0,0 +1,106 @@
+---
+title: extends
+slug: Web/JavaScript/Reference/Classes/extends
+tags:
+ - Classes
+ - ECMAScript 2015
+ - JavaScript
+translation_of: Web/JavaScript/Reference/Classes/extends
+---
+<div>{{jsSidebar("Classes")}}</div>
+
+<p><strong><code>extends</code></strong>关键字用于<a href="/zh-CN/docs/Web/JavaScript/Reference/Statements/class">类声明</a>或者<a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/class">类表达式</a>中,以创建一个类,该类是另一个类的子类。</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox">class ChildClass extends ParentClass { ... }</pre>
+
+<h2 id="描述">描述</h2>
+
+<p><code>extends</code>关键字用来创建一个普通类或者内建对象的子类。</p>
+
+<p><font face="Open Sans, Arial, sans-serif">继承的</font><code>.prototype</code>必须是一个{{jsxref("Object")}} 或者 {{jsxref("null")}}。</p>
+
+<h2 id="示例">示例</h2>
+
+<h3 id="使用_extends">使用 <code>extends</code></h3>
+
+<p>第一个例子是根据名为 <code style="font-style: normal;">Polygon</code> 类创建一个名为<code>Square</code>的类。这个例子是从这个<a href="https://googlechrome.github.io/samples/classes-es6/index.html">在线演示</a>中提取出来的。</p>
+
+<pre class="brush: js">class Square extends Polygon {
+ constructor(length) {
+ // Here, it calls the parent class' constructor with lengths
+ // provided for the Polygon's width and height
+ super(length, length);
+ // Note: In derived classes, super() must be called before you
+ // can use 'this'. Leaving this out will cause a reference error.
+ this.name = 'Square';
+ }
+
+ get area() {
+ return this.height * this.width;
+ }
+}</pre>
+
+<h3 id="使用_extends与内置对象">使用 <code>extends</code>与内置对象</h3>
+
+<p>这个示例继承了内置的{{jsxref("Date")}}对象。这个例子是从这个<a href="https://googlechrome.github.io/samples/classes-es6/index.html">在线演示</a>中提取出来的。</p>
+
+<pre class="brush: js">class myDate extends Date {
+ constructor() {
+ super();
+ }
+
+ getFormattedDate() {
+ var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
+ return this.getDate() + "-" + months[this.getMonth()] + "-" + this.getFullYear();
+ }
+}</pre>
+
+<h3 id="扩展_null">扩展 <code>null</code></h3>
+
+<p>可以像扩展普通类一样扩展{{jsxref("null")}},但是新对象的原型将不会继承 {{jsxref("Object.prototype")}}。</p>
+
+<pre class="brush: js">class nullExtends extends null {
+ constructor() {}
+}
+
+Object.getPrototypeOf(nullExtends); // Function.prototype
+Object.getPrototypeOf(nullExtends.prototype) // null
+
+new nullExtends(); //ReferenceError: this is not defined</pre>
+
+<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', 'extends')}}</td>
+ <td>{{Spec2('ES2015')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-class-definitions', 'extends')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容">浏览器兼容</h2>
+
+<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div>
+
+<p>{{Compat("javascript.classes.extends")}}</p>
+
+<h2 id="扩展阅读">扩展阅读</h2>
+
+<ul>
+ <li><a href="/zh-CN/docs/Web/JavaScript/Reference/Classes">类</a></li>
+ <li><a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/super">super</a></li>
+</ul>