From de5c456ebded0e038adbf23db34cc290c8829180 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 14:49:24 +0100 Subject: unslug pl: move --- .../reference/classes/constructor/index.html | 188 +++++++++++++++++++++ .../reference/classes/konstruktor/index.html | 188 --------------------- 2 files changed, 188 insertions(+), 188 deletions(-) create mode 100644 files/pl/web/javascript/reference/classes/constructor/index.html delete mode 100644 files/pl/web/javascript/reference/classes/konstruktor/index.html (limited to 'files/pl/web/javascript/reference/classes') diff --git a/files/pl/web/javascript/reference/classes/constructor/index.html b/files/pl/web/javascript/reference/classes/constructor/index.html new file mode 100644 index 0000000000..353adecd19 --- /dev/null +++ b/files/pl/web/javascript/reference/classes/constructor/index.html @@ -0,0 +1,188 @@ +--- +title: Konstruktor +slug: Web/JavaScript/Reference/Classes/Konstruktor +tags: + - Classes + - JavaScript + - Language feature +translation_of: Web/JavaScript/Reference/Classes/constructor +--- +
{{jsSidebar("Classes")}}
+ +

Konstruktor jest specjalną metodą tworzenia i inicjowania obiektu utworzonego w klasie.

+ +

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

+ +

Składnia

+ +
constructor([arguments]) { ... }
+ +

Opis

+ +

Konstruktor umożliwia zdefiniowanie inicjalizacji obiektu, która musi się wykonać, zanim będzie można wywołać metody obiektu.

+ +
class Person {
+
+  constructor(name) {
+    this.name = name;
+  }
+
+  introduce() {
+    console.log(`Hello, my name is ${this.name}`);
+  }
+
+}
+
+const otto = new Person('Otto');
+
+otto.introduce();
+ +

Jeśli niestandardowy konstruktor nie został podany, to domyślny konstruktor będzie użyty. Dla klas bazowych konstruktor domyślny jest pusty:

+ +
constructor() {}
+ +

Dla klas pochodnych domyślny konstruktor wywołuje konstruktor klasy nadrzędnej:

+ +
constructor(...args) {
+  super(...args);
+}
+ +

Pozwala to na działanie takiego kodu:

+ +
class ValidationError extends Error {
+
+  printCustomerMessage() {
+    return `Validation failed :-( (details: ${this.message})`;
+  }
+
+}
+
+try {
+  throw new ValidationError("Not a valid phone number");
+} catch (error) {
+   if (error instanceof ValidationError) {
+    console.log(error.name); // This is Error instead of ValidationError!
+    console.log(error.printCustomerMessage());
+  } else {
+    console.log('Unknown error', error);
+    throw error;
+  }
+}
+ +

Klasa ValidationError nie musi mieć niestandardowego konstruktora, ponieważ domyślny konstruktor wywołuje konstruktor klasy Error.

+ +

Jeśli jednak klasa ValidationError ma niestandardowy konstruktor, to musi on wywoływać konstruktor klasy nadrzędnej przy użyciu super:

+ +
class ValidationError extends Error {
+
+  constructor(message) {
+    super(message);  // call parent class constructor
+    this.name = 'ValidationError';
+    this.code = '42';
+  }
+
+  printCustomerMessage() {
+     return `Validation failed :-( (details: ${this.message}, code: ${this.code})`;
+  }
+
+}
+
+try {
+  throw new ValidationError("Not a valid phone number");
+} catch (error) {
+   if (error instanceof ValidationError) {
+    console.log(error.name); // Now this is ValidationError!
+    console.log(error.printCustomerMessage());
+  } else {
+    console.log('Unknown error', error);
+    throw error;
+  }
+}
+ +

Wewnątrz klasy może być tylko jedna metoda nazwana constructor. Jeżeli constructor wystąpi więcej niż jeden raz, to wygeneruje błąd {{jsxref("SyntaxError")}}.

+ +

Przykłady

+ +

Używanie konstruktora

+ +

Fragment kodu pochodzi z classes sample (live demo).

+ +
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;
+  }
+
+  set area(value) {
+    this.area = value;
+  }
+}
+ +

Inny przykład

+ +

W tym przykładzie klasa Square jest zmieniona — ale konstruktor klasy Polygon nadal jest wywoływany przy tworzeniu nowej instancji klasy Square.

+ +
class Polygon {
+    constructor() {
+        this.name = "Polygon";
+    }
+}
+
+class Square extends Polygon {
+    constructor() {
+        super();
+    }
+}
+
+class Rectangle {}
+
+Object.setPrototypeOf(Square.prototype, Rectangle.prototype);
+
+console.log(Object.getPrototypeOf(Square.prototype) === Polygon.prototype); //false
+console.log(Object.getPrototypeOf(Square.prototype) === Rectangle.prototype); //true
+
+let newInstance = new Square();
+console.log(newInstance.name); //Polygon
+ +

Specyfikacje

+ + + + + + + + + + + + + + + + + + + +
SpecyfikacjaStatusKomentarz
{{SpecName('ES2015', '#sec-static-semantics-constructormethod', 'Constructor Method')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-static-semantics-constructormethod', 'Constructor Method')}}{{Spec2('ESDraft')}}
+ +

Kompatybilność

+ +

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

+ +

Zobacz też

+ + diff --git a/files/pl/web/javascript/reference/classes/konstruktor/index.html b/files/pl/web/javascript/reference/classes/konstruktor/index.html deleted file mode 100644 index 353adecd19..0000000000 --- a/files/pl/web/javascript/reference/classes/konstruktor/index.html +++ /dev/null @@ -1,188 +0,0 @@ ---- -title: Konstruktor -slug: Web/JavaScript/Reference/Classes/Konstruktor -tags: - - Classes - - JavaScript - - Language feature -translation_of: Web/JavaScript/Reference/Classes/constructor ---- -
{{jsSidebar("Classes")}}
- -

Konstruktor jest specjalną metodą tworzenia i inicjowania obiektu utworzonego w klasie.

- -

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

- -

Składnia

- -
constructor([arguments]) { ... }
- -

Opis

- -

Konstruktor umożliwia zdefiniowanie inicjalizacji obiektu, która musi się wykonać, zanim będzie można wywołać metody obiektu.

- -
class Person {
-
-  constructor(name) {
-    this.name = name;
-  }
-
-  introduce() {
-    console.log(`Hello, my name is ${this.name}`);
-  }
-
-}
-
-const otto = new Person('Otto');
-
-otto.introduce();
- -

Jeśli niestandardowy konstruktor nie został podany, to domyślny konstruktor będzie użyty. Dla klas bazowych konstruktor domyślny jest pusty:

- -
constructor() {}
- -

Dla klas pochodnych domyślny konstruktor wywołuje konstruktor klasy nadrzędnej:

- -
constructor(...args) {
-  super(...args);
-}
- -

Pozwala to na działanie takiego kodu:

- -
class ValidationError extends Error {
-
-  printCustomerMessage() {
-    return `Validation failed :-( (details: ${this.message})`;
-  }
-
-}
-
-try {
-  throw new ValidationError("Not a valid phone number");
-} catch (error) {
-   if (error instanceof ValidationError) {
-    console.log(error.name); // This is Error instead of ValidationError!
-    console.log(error.printCustomerMessage());
-  } else {
-    console.log('Unknown error', error);
-    throw error;
-  }
-}
- -

Klasa ValidationError nie musi mieć niestandardowego konstruktora, ponieważ domyślny konstruktor wywołuje konstruktor klasy Error.

- -

Jeśli jednak klasa ValidationError ma niestandardowy konstruktor, to musi on wywoływać konstruktor klasy nadrzędnej przy użyciu super:

- -
class ValidationError extends Error {
-
-  constructor(message) {
-    super(message);  // call parent class constructor
-    this.name = 'ValidationError';
-    this.code = '42';
-  }
-
-  printCustomerMessage() {
-     return `Validation failed :-( (details: ${this.message}, code: ${this.code})`;
-  }
-
-}
-
-try {
-  throw new ValidationError("Not a valid phone number");
-} catch (error) {
-   if (error instanceof ValidationError) {
-    console.log(error.name); // Now this is ValidationError!
-    console.log(error.printCustomerMessage());
-  } else {
-    console.log('Unknown error', error);
-    throw error;
-  }
-}
- -

Wewnątrz klasy może być tylko jedna metoda nazwana constructor. Jeżeli constructor wystąpi więcej niż jeden raz, to wygeneruje błąd {{jsxref("SyntaxError")}}.

- -

Przykłady

- -

Używanie konstruktora

- -

Fragment kodu pochodzi z classes sample (live demo).

- -
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;
-  }
-
-  set area(value) {
-    this.area = value;
-  }
-}
- -

Inny przykład

- -

W tym przykładzie klasa Square jest zmieniona — ale konstruktor klasy Polygon nadal jest wywoływany przy tworzeniu nowej instancji klasy Square.

- -
class Polygon {
-    constructor() {
-        this.name = "Polygon";
-    }
-}
-
-class Square extends Polygon {
-    constructor() {
-        super();
-    }
-}
-
-class Rectangle {}
-
-Object.setPrototypeOf(Square.prototype, Rectangle.prototype);
-
-console.log(Object.getPrototypeOf(Square.prototype) === Polygon.prototype); //false
-console.log(Object.getPrototypeOf(Square.prototype) === Rectangle.prototype); //true
-
-let newInstance = new Square();
-console.log(newInstance.name); //Polygon
- -

Specyfikacje

- - - - - - - - - - - - - - - - - - - -
SpecyfikacjaStatusKomentarz
{{SpecName('ES2015', '#sec-static-semantics-constructormethod', 'Constructor Method')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-static-semantics-constructormethod', 'Constructor Method')}}{{Spec2('ESDraft')}}
- -

Kompatybilność

- -

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

- -

Zobacz też

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