diff options
Diffstat (limited to 'files/pl/web/javascript/reference/operators/new.target')
-rw-r--r-- | files/pl/web/javascript/reference/operators/new.target/index.html | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/files/pl/web/javascript/reference/operators/new.target/index.html b/files/pl/web/javascript/reference/operators/new.target/index.html new file mode 100644 index 0000000000..8b60074e92 --- /dev/null +++ b/files/pl/web/javascript/reference/operators/new.target/index.html @@ -0,0 +1,134 @@ +--- +title: new.target +slug: Web/JavaScript/Referencje/Operatory/new.target +translation_of: Web/JavaScript/Reference/Operators/new.target +--- +<div>{{JSSidebar("Operators")}}</div> + +<p><strong><code>new.target</code></strong> pozwala sprawdzić czy konstruktor lub funkcja była zainicjalizowana używając operatora <a href="/en-US/docs/Web/JavaScript/Reference/Operators/new">new</a>. Kiedy w konstruktorze lub funkcji zainicjalizowany jest operatora <a href="/en-US/docs/Web/JavaScript/Reference/Operators/new">new</a>, <code>new.target</code> zwraca referencje do konstruktora lub funkcji. W normalnym wywołaniu funkcji, <code>new.target</code> jest {{jsxref("undefined")}}.</p> + +<h2 id="Syntax">Syntax</h2> + +<pre class="syntaxbox">new.target</pre> + +<h2 id="Opis">Opis</h2> + +<p><code>new.target</code> składa się ze słowa <code>"new</code>", kropki i pola o nazwie <code>"target"</code>. Przeważnie "<code>new."</code> jest kontekstem dostępu do pola, ale w tym przypadku "new." nie jest tak naprawdę obiektem. W wywołaniu konstruktora, <code>new.target</code> nawiązuje do konstruktora wywołanego przez <code>new</code> więc "<code>new.</code>" staje się wirtualnym kontekstem.</p> + +<p><code><font face="Open Sans, Arial, sans-serif">Pole </font>new.target</code> jest meta polem które jest dostępne dla wszystkich funkcji. W <a href="/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions">arrow functions</a>, <code>new.target</code> nawiązuje do <code>new.target</code> otaczającej funkcji.</p> + +<h2 id="Przykłady">Przykłady</h2> + +<h3 id="new.target_w_wywołaniu_funkcji">new.target w wywołaniu funkcji</h3> + +<p>W normalnym wywołaniu funkcji (w przeciwieństwie do wywołania konstruktora), <code>new.target</code> jest {{jsxref("undefined")}}. Pozwala Ci to stwierdzić, czy funkcja została wywołana wraz z <a href="/en-US/docs/Web/JavaScript/Reference/Operators/new">new</a> jako konstruktor.</p> + +<pre class="brush: js">function Foo() { + if (!new.target) throw "Foo() must be called with new"; + console.log("Foo instantiated with new"); +} + +Foo(); // throws "Foo() must be called with new" +new Foo(); // logs "Foo instantiated with new" +</pre> + +<h3 id="new.target_w_konstruktorze">new.target w konstruktorze</h3> + +<p>W konstruktorach klasy, <code>new.target</code> nawiązuje do konstruktora który został bezpośrednio wywołany z <a href="/en-US/docs/Web/JavaScript/Reference/Operators/new">new</a>. Dzieje się tak również w przypadku kiedy konstruktor znajduje się w klasie rodzica, i został wywołany z konstruktorze dziecka.</p> + +<pre class="brush: js">class A { + constructor() { + console.log(new.target.name); + } +} + +class B extends A { constructor() { super(); } } + +var a = new A(); // logs "A" +var b = new B(); // logs "B" +</pre> + +<h2 id="Specyfikacja">Specyfikacja</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('ES6', '#sec-built-in-function-objects', 'Built-in Function Objects')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-built-in-function-objects', 'Built-in Function Objects')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Kompatybilność_przeglądarek">Kompatybilność przeglądarek</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatChrome(46.0)}}</td> + <td>{{CompatGeckoDesktop(41)}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Android Webview</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + <th>Chrome for Android</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>{{CompatChrome(46.0)}}</td> + <td>{{CompatGeckoMobile(41)}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatChrome(46.0)}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Zobacz_również">Zobacz również</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Functions">Functions</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Classes">Classes</a></li> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/new">new</a></code></li> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/this">this</a></code></li> +</ul> |