From 074785cea106179cb3305637055ab0a009ca74f2 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:52 -0500 Subject: initial commit --- .../reference/classes/extends/index.html | 88 ++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 files/pl/web/javascript/reference/classes/extends/index.html (limited to 'files/pl/web/javascript/reference/classes/extends/index.html') diff --git a/files/pl/web/javascript/reference/classes/extends/index.html b/files/pl/web/javascript/reference/classes/extends/index.html new file mode 100644 index 0000000000..6b25a766e5 --- /dev/null +++ b/files/pl/web/javascript/reference/classes/extends/index.html @@ -0,0 +1,88 @@ +--- +title: extends +slug: Web/JavaScript/Reference/Classes/extends +tags: + - Classes + - ECMAScript 2015 + - JavaScript +translation_of: Web/JavaScript/Reference/Classes/extends +--- +
{{jsSidebar("Classes")}}
+ +

Słowo kluczowe extends jest używane w deklaracjach klas lub wyrażeniach class do tworzenia klasy jako elementu potomnego innej klasy.

+ +
{{EmbedInteractiveExample("pages/js/classes-extends.html")}}
+ + + +

Składnia

+ +
class ChildClass extends ParentClass { ... }
+ +

Opis

+ +

Słowo kluczowe extends może być użyte do dziedziczenia po niestandardowych klasach lub standardowych obiektach wbudowanych.

+ +

Prototypem rozszerzenia musi być {{jsxref("Object")}} lub {{jsxref("null")}}.

+ +

Przykłady

+ +

Zastosowanie extends

+ +

Pierwszy przykład tworzy klasę Square rozszerzającą klasę Polygon. live demo (source).

+ +
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;
+  }
+}
+ +

Zastosowanie extends z obiektami wbudowanymi

+ +

Poniższy przykład rozszerza wbudowany obiekt {{jsxref("Date")}}. live demo (source).

+ +
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();
+  }
+}
+
+ +

Specyfikacje

+ + + + + + + + + + +
Specyfikacja
{{SpecName('ESDraft', '#sec-class-definitions', 'extends')}}
+ +

Kompatybilność

+ + + +

{{Compat("javascript.classes.extends")}}

+ +

Zobacz też

+ + -- cgit v1.2.3-54-g00ecf