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(Thừa kế)
slug: Web/JavaScript/Reference/Classes/extends
translation_of: Web/JavaScript/Reference/Classes/extends
---
<div>{{jsSidebar("Classes")}}</div>
<p><code><font face="Arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;">Từ khóa </span></font><strong>extends</strong></code> được sử dụng trong khai báo <a href="/en-US/docs/Web/JavaScript/Reference/Statements/class">class</a> hoặc trong <a href="/en-US/docs/Web/JavaScript/Reference/Operators/class">class expressions</a> để tạo ra 1 class con là con của class khác</p>
<div>{{EmbedInteractiveExample("pages/js/classes-extends.html", "taller")}}</div>
<h2 id="Cú_pháp">Cú pháp</h2>
<pre class="syntaxbox">class ChildClass extends ParentClass { ... }</pre>
<h2 id="Mô_tả">Mô tả</h2>
<p>Từ khó <code>extends</code> có thể được sử dụng để tạo ra class con từ những class mà chúng ta tạo ra hoặc những class sẵn có</p>
<p><code><font face="Arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;">Thuộc tính </span></font>.prototype</code> của lớp được kế thừa phải trả về giá trị là {{jsxref("Object")}} hoặc {{jsxref("null")}}.</p>
<h2 id="Ví_dụ">Ví dụ</h2>
<h3 id="Sử_dụng_extends">Sử dụng <code>extends</code></h3>
<p>Ở ví dụ đầu tiên tạo ra 1 class <code>Square</code> từ class <code>Polygon</code>. Ví dụ này được lấy từ đây <a href="https://github.com/GoogleChrome/samples/blob/gh-pages/classes-es6/index.html">(source)</a>.</p>
<pre class="brush: js">class Square extends Polygon {
constructor(length) {
// Ở đây, gọi 1 constructor từ class cha với tham số truyền vào là length
// cung cấp chiều rộng vào chiều cao của class Polygon
super(length, length);
// Chú ý: trong những class con, super() phải được gọi trước khi
// bạn có thể sử dụng từ khóa 'this'. Nếu không sẽ gây ra lỗi tham chiếu
this.name = 'Square';
}
get area() {
return this.height * this.width;
}
}</pre>
<h3 id="Sử_dụng_extends_với_những_object_có_sẵn"><code><font face="x-locale-heading-primary, zillaslab, Palatino, Palatino Linotype, x-locale-heading-secondary, serif"><span style="background-color: #333333;">Sử dụng </span></font>extends</code> với những object có sẵn</h3>
<p>Trong ví dụ này chúng ta thừa kế từ object có sẵn là {{jsxref("Date")}} . Ví dụ này được lấy từ đây<a href="https://github.com/GoogleChrome/samples/blob/gh-pages/classes-es6/index.html">(source)</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="Thừa_kế_từ_null">Thừa kế từ <code>null</code></h3>
<p>Thừa kế từ {{jsxref("null")}} cũng giống như class thông thường, ngoại trừ rằng các prototype của object sẽ không được thừa kế từ {{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="Thông_số_kĩ_thuật">Thông số kĩ thuật</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Thông số</th>
<th scope="col">Trạng thái</th>
<th scope="col">Bình luận</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="Trình_duyệt_tương_thích">Trình duyệt tương thích</h2>
<p>{{Compat("javascript.classes.extends")}}</p>
<h2 id="Bài_viết_liên_quan">Bài viết liên quan</h2>
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Classes">Classes</a></li>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/super">super</a></li>
<li><a href="https://medium.com/beginners-guide-to-mobile-web-development/super-and-extends-in-javascript-es6-understanding-the-tough-parts-6120372d3420">Anurag Majumdar - Super & Extends in JavaScript</a></li>
</ul>
|