aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/classes/extends/index.html
blob: 37223f83ee847125c2ac1ce0f043690b2c57f6cf (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
---
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>继承的<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-CN/docs/Web/JavaScript/Reference/Classes"></a></li>
 <li><a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/super">super</a></li>
</ul>