aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/javascript/reference/classes/extends/index.html
blob: ce729e552b5b610ded7ec4dcc8c9968a5f6e8c80 (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
---
title: extends
slug: Web/JavaScript/Reference/Classes/extends
translation_of: Web/JavaScript/Reference/Classes/extends
---
<div>{{jsSidebar("Classes")}}</div>

<p><strong><code>extends</code></strong> 關鍵字被使用於<a href="/en-US/docs/Web/JavaScript/Reference/Statements/class">類別(class)宣告</a><a href="/en-US/docs/Web/JavaScript/Reference/Operators/class">類別(class)表達式</a>中來建立擴展的子類別 。</p>

<div>{{EmbedInteractiveExample("pages/js/classes-extends.html", "taller")}}</div>



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

<pre class="syntaxbox">class ChildClass extends ParentClass { ... }
</pre>

<h2 id="解釋">解釋</h2>

<p><code>extends</code> 關鍵字可用於建立一個自訂類別或內建類別的子類別。</p>

<p>其繼承之原型 <code>.prototype</code> 必須是 {{jsxref("Object")}} 或 {{jsxref("null")}}</p>

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

<h3 id="使用_extends">使用 <code>extends</code></h3>

<p>第一個範例是根據 <code>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>



<p>{{Compat("javascript.classes.extends")}}</p>

<h2 id="參見">參見</h2>

<ul>
 <li><a href="/zh-TW/docs/Web/JavaScript/Reference/Classes">Classes</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/super">super</a></li>
</ul>