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
|
---
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>Słowo kluczowe <strong><code>extends</code></strong> jest używane w <a href="/pl/docs/Web/JavaScript/Referencje/Polecenia/class">deklaracjach klas</a> lub <a href="/en-US/docs/Web/JavaScript/Reference/Operators/class">wyrażeniach class</a> do tworzenia klasy jako elementu potomnego innej klasy.</p>
<div>{{EmbedInteractiveExample("pages/js/classes-extends.html")}}</div>
<h2 id="Składnia">Składnia</h2>
<pre class="syntaxbox notranslate">class ChildClass extends ParentClass { ... }</pre>
<h2 id="Opis">Opis</h2>
<p>Słowo kluczowe <code>extends</code> może być użyte do dziedziczenia po niestandardowych klasach lub standardowych obiektach wbudowanych.</p>
<p>Prototypem rozszerzenia musi być {{jsxref("Object")}} lub {{jsxref("null")}}.</p>
<h2 id="Przykłady">Przykłady</h2>
<h3 id="Zastosowanie_extends">Zastosowanie <code>extends</code></h3>
<p>Pierwszy przykład tworzy klasę <code>Square</code> rozszerzającą klasę <code>Polygon</code>. <a href="https://googlechrome.github.io/samples/classes-es6/index.html">live demo</a> <a href="https://github.com/GoogleChrome/samples/blob/gh-pages/classes-es6/index.html">(source)</a>.</p>
<pre class="brush: js notranslate">class Square extends Polygon {
constructor(length) {
// Wywołanie konstruktora klasy nadrzędnej
// określenie szerokości i wysokości wielokątu
super(length, length);
// Uwaga: W pochodnych klasach, super() musi być wywołane wcześniej niż
// pierwsze użycie 'this'. W przeciwnym wypadku pojawi się błąd odniesienia.
this.name = 'Square';
}
get area() {
return this.height * this.width;
}
}</pre>
<h3 id="Zastosowanie_extends_z_obiektami_wbudowanymi">Zastosowanie <code>extends</code> z obiektami wbudowanymi</h3>
<p>Poniższy przykład rozszerza wbudowany obiekt {{jsxref("Date")}}. <a href="https://googlechrome.github.io/samples/classes-es6/index.html">live demo</a> <a href="https://github.com/GoogleChrome/samples/blob/gh-pages/classes-es6/index.html">(source)</a>.</p>
<pre class="brush: js notranslate">class myDate extends Date {
getFormattedDate() {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return this.getDate() + '-' + months[this.getMonth()] + '-' + this.getFullYear();
}
}
</pre>
<h2 id="Specyfikacje">Specyfikacje</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specyfikacja</th>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-class-definitions', 'extends')}}</td>
</tr>
</tbody>
</table>
<h2 id="Kompatybilność">Kompatybilność</h2>
<p>{{Compat("javascript.classes.extends")}}</p>
<h2 id="Zobacz_też">Zobacz też</h2>
<ul>
<li><a href="/pl/docs/Web/JavaScript/Reference/Classes">Classes</a></li>
<li><a href="/pl/docs/Web/JavaScript/Reference/Classes/Konstruktor">Konstruktor</a></li>
<li><a href="/pl/docs/Web/JavaScript/Referencje/Operatory/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>
|