|
1 |
| -# The modern mode, "use strict" |
| 1 | +# Nowoczesny tryb, "use strict" |
2 | 2 |
|
3 |
| -For a long time, JavaScript evolved without compatibility issues. New features were added to the language while old functionality didn't change. |
| 3 | +Przez długi czas JavaScript rozwijał się bez problemów z kompatybilnością. Dodawane były nowe funkcjonalności przy zachowaniu starych. |
4 | 4 |
|
5 |
| -That had the benefit of never breaking existing code. But the downside was that any mistake or an imperfect decision made by JavaScript's creators got stuck in the language forever. |
| 5 | +To miało tę zaletę, że istniejący kod zawsze działał. Ale minusem było to, że każda niedoskonałość ze strony twórców pozostawała w języku na zawsze. |
6 | 6 |
|
7 |
| -This was the case until 2009 when ECMAScript 5 (ES5) appeared. It added new features to the language and modified some of the existing ones. To keep the old code working, most such modifications are off by default. You need to explicitly enable them with a special directive: `"use strict"`. |
| 7 | +Tak było do 2009 roku, kiedy pojawił się standard ECMAScript 5 (ES5). W tej wersji dodano nowe funkcjonalności oraz zmodyfikowano niektóre już istniejące. Żeby stary kod nadal mógł działać, domyślnie wyłączona jest większość modyfikacji. Aby je włączyć, należy podać specjalną dyrektywę: `"use strict"`. |
8 | 8 |
|
9 | 9 | ## "use strict"
|
10 | 10 |
|
11 |
| -The directive looks like a string: `"use strict"` or `'use strict'`. When it is located at the top of a script, the whole script works the "modern" way. |
| 11 | +Dyrektywa wygląda jak zwykły ciąg znaków: `"use strict"` lub `'use strict'`. Jeśli jest ona umieszczona na samym początku skryptu, wtedy cały skrypt działa w "nowoczesnym" trybie ścisłym. |
12 | 12 |
|
13 |
| -For example: |
| 13 | +Dla przykładu: |
14 | 14 |
|
15 | 15 | ```js
|
16 | 16 | "use strict";
|
17 | 17 |
|
18 |
| -// this code works the modern way |
| 18 | +// ten kod zadziała w nowoczesnym trybie |
19 | 19 | ...
|
20 | 20 | ```
|
21 | 21 |
|
22 |
| -We will learn functions (a way to group commands) soon. Looking ahead, let's note that `"use strict"` can be put at the beginning of the function body instead of the whole script. Doing that enables strict mode in that function only. But usually, people use it for the whole script. |
| 22 | +W niedługim czasie nauczysz się funkcji (sposobu łączenia instrukcji w grupy). Zauważ, że trybu ścisłego `"use strict"` można użyć na początku funkcji zamiast umieszczać na początku całego skryptu. Jeśli tak zrobisz, tryb ścisły będzie obowiązywał tylko w wybranej funkcji. Zazwyczaj jednak stosuje się go dla całego skryptu. |
23 | 23 |
|
24 | 24 |
|
25 |
| -````warn header="Ensure that \"use strict\" is at the top" |
26 |
| -Please make sure that `"use strict"` is at the top of your scripts, otherwise strict mode may not be enabled. |
| 25 | +````warn header="Upewnij się, że \"use strict\" jest na samej górze" |
| 26 | +Upewnij się, że dyrektywa `"use strict"` znajduje się na samej górze skryptu. W przeciwnym wypadku tryb ten nie zostanie włączony. |
27 | 27 |
|
28 |
| -Strict mode isn't enabled here: |
| 28 | +Tryb ścisły nie będzie działał w poniższym przypadku: |
29 | 29 |
|
30 | 30 | ```js no-strict
|
31 |
| -alert("some code"); |
32 |
| -// "use strict" below is ignored--it must be at the top |
| 31 | +alert("jakiś kod"); |
| 32 | +// Dyrektywa "use strict" jest ignorowana -- musi zostać zadeklarowana na samej górze |
33 | 33 |
|
34 | 34 | "use strict";
|
35 | 35 |
|
36 |
| -// strict mode is not activated |
| 36 | +// tryb ścisły nie jest włączony |
37 | 37 | ```
|
38 | 38 |
|
39 |
| -Only comments may appear above `"use strict"`. |
| 39 | +Tylko komentarze mogą znajdować się powyżej deklaracji `"use strict"`. |
40 | 40 | ````
|
41 | 41 |
|
42 |
| -```warn header="There's no way to cancel `use strict`" |
43 |
| -There is no directive like `"no use strict"` that reverts the engine to old behavior. |
| 42 | +```warn header="Nie ma możliwości anulowania `use strict`" |
| 43 | +Nie ma takiej dyrektywy jak `"no use strict"`, która przywraca wcześniejsze działanie silnika JavaScript. |
44 | 44 |
|
45 |
| -Once we enter strict mode, there's no return. |
| 45 | +Jeśli włączysz tryb ścisły, nie ma już odwrotu. |
46 | 46 | ```
|
47 | 47 |
|
48 |
| -## Browser console |
| 48 | +## Konsola przeglądarki |
49 | 49 |
|
50 |
| -For the future, when you use a browser console to test features, please note that it doesn't `use strict` by default. |
| 50 | +Tak na przyszłość, jeśli chcesz używać konsoli przeglądarki do testowania swoich funkcjonalności, pamiętaj, że tryb ścisły `use strict` nie jest domyślnie włączony. |
51 | 51 |
|
52 |
| -Sometimes, when `use strict` makes a difference, you'll get incorrect results. |
| 52 | +W niektórych sytuacjach użycie tego trybu skutkuje otrzymywaniem niewłaściwych rezultatów. |
53 | 53 |
|
54 |
| -You can try to press `key:Shift+Enter` to input multiple lines, and put `use strict` on top, like this: |
| 54 | +Naciśnij `key:Shift+Enter`, żeby wpisać kod w wielu liniach i wpisz `use strict` na samej górze, jak tutaj: |
55 | 55 |
|
56 | 56 | ```js
|
57 |
| -'use strict'; <Shift+Enter for a newline> |
58 |
| -// ...your code |
59 |
| -<Enter to run> |
| 57 | +'use strict'; <Shift+Enter dla nowej linii> |
| 58 | +// ...Twój kod |
| 59 | +<Enter, żeby uruchomić> |
60 | 60 | ```
|
61 | 61 |
|
62 |
| -It works in most browsers, namely Firefox and Chrome. |
| 62 | +Działa na większości przeglądarek, a z pewnością na Firefoksie i Chromie. |
63 | 63 |
|
64 |
| -If it doesn't, the most reliable way to ensure `use strict` would be to input the code into console like this: |
| 64 | +Jeśli jednak z jakiegoś powodu nie zadziała, wystarczy że użyjesz poniższego kodu: |
65 | 65 |
|
66 | 66 | ```js
|
67 | 67 | (function() {
|
68 | 68 | 'use strict';
|
69 | 69 |
|
70 |
| - // ...your code... |
| 70 | + // ...twój kod... |
71 | 71 | })()
|
72 | 72 | ```
|
73 | 73 |
|
74 |
| -## Always "use strict" |
| 74 | +## Zawsze używaj "use strict" |
75 | 75 |
|
76 |
| -We have yet to cover the differences between strict mode and the "default" mode. |
| 76 | +Musimy jeszcze omówić różnice pomiędzy trybem ścisłym a trybem domyślnym. |
77 | 77 |
|
78 |
| -In the next chapters, as we learn language features, we'll note the differences between the strict and default modes. Luckily, there aren't many and they actually make our lives better. |
| 78 | +W następnych rozdziałach, gdy będziemy poznawać funkcjonalności języka, poznamy różnice pomiędzy tymi trybami. Na szczęście nie ma ich wiele, ale czynią nasze programistyczne życie lepszym. |
79 | 79 |
|
80 |
| -For now, it's enough to know about it in general: |
| 80 | +Póki co, wystarczy, jeśli wiesz, że: |
81 | 81 |
|
82 |
| -1. The `"use strict"` directive switches the engine to the "modern" mode, changing the behavior of some built-in features. We'll see the details later in the tutorial. |
83 |
| -2. Strict mode is enabled by placing `"use strict"` at the top of a script or function. Several language features, like "classes" and "modules", enable strict mode automatically. |
84 |
| -3. Strict mode is supported by all modern browsers. |
85 |
| -4. We recommended always starting scripts with `"use strict"`. All examples in this tutorial assume strict mode unless (very rarely) specified otherwise. |
| 82 | +1. Dyrektywa `"use strict"` przełącza silnik JavaScript w tryb "nowoczesny" (ścisły). Zmienia to zachowanie wbudowanych funkcjonalności. O szczegółach dowiesz się z kolejnych rozdziałów. |
| 83 | +2. Tryb ścisły jest włączany, gdy umieścisz dyrektywę `"use strict"` na początku skryptu lub funkcji. Niektóre z funkcjonalności języka, takie jak "klasy" czy "moduły", włączają tryb ścisły wewnątrz siebie automatycznie. |
| 84 | +3. Tryb ścisły jest wspierany przez wszystkie nowoczesne przeglądarki internetowe. |
| 85 | +4. Zalecamy zaczynanie wszystkich skryptów od dyrektywy `"use strict"`. Wszystkie przykłady w tym samouczku zakładają, że tryb jest włączony, chyba że (bardzo rzadko) określono inaczej. |
0 commit comments