|
1 |
| -# Methods of primitives |
2 |
| - |
3 |
| -JavaScript allows us to work with primitives (strings, numbers, etc.) as if they were objects. They also provide methods to call as such. We will study those soon, but first we'll see how it works because, of course, primitives are not objects (and here we will make it even clearer). |
4 |
| - |
5 |
| -Let's look at the key distinctions between primitives and objects. |
6 |
| - |
7 |
| -A primitive |
8 |
| - |
9 |
| -- Is a value of a primitive type. |
10 |
| -- There are 7 primitive types: `string`, `number`, `bigint`, `boolean`, `symbol`, `null` and `undefined`. |
11 |
| - |
12 |
| -An object |
13 |
| - |
14 |
| -- Is capable of storing multiple values as properties. |
15 |
| -- Can be created with `{}`, for instance: `{name: "John", age: 30}`. There are other kinds of objects in JavaScript: functions, for example, are objects. |
16 |
| - |
17 |
| -One of the best things about objects is that we can store a function as one of its properties. |
18 |
| - |
| 1 | +# Metody typów podstawowych |
| 2 | + |
| 3 | +JavaScript pozwala na pracę z typami podstawowymi (ciągi znaków, liczby, itd.) w taki sam sposób jakby były obiektami. Dostarczają one także metod, które można wywołać tak samo jak w przypadku obiektów. Wkrótce przestudiujemy te metody, ale najpierw sprawdźmy jak wartości podstawowe działają, ponieważ oczywiście typy podstawowe nie są obiektami (za chwilę szerzej to wyjaśnimy). |
| 4 | + |
| 5 | +Spójrzmy na kluczowe różnice pomiędzy typami podstawowymi, a obiektami. |
| 6 | + |
| 7 | +Typ podstawowy |
| 8 | + |
| 9 | +- Jest wartością typu podstawowego. |
| 10 | +- Wyróżniamy 7 typów podstawowych: `tekstowy`, `liczbowy`, `bigint`, `logiczny`, `symbol`, `null` and `undefined`. |
| 11 | + |
| 12 | +Obiekt |
| 13 | + |
| 14 | +- Jest w stanie przechowywać wiele wartości jako własności. |
| 15 | +- Można go stworzyć za pomocą`{}`, na przykład: `{name: "Janek", age: 30}`. Są także inne rodzaje obiektów w JavaScript: na przykład funkcje są obiektami. |
| 16 | + |
| 17 | +Jedną z najlepszych rzeczy w obiektach jest to, że możemy przechowywać funkcje jako własność obiektu. |
| 18 | + |
19 | 19 | ```js run
|
20 |
| -let john = { |
21 |
| - name: "John", |
| 20 | +let janek = { |
| 21 | + name: "Jan", |
22 | 22 | sayHi: function() {
|
23 |
| - alert("Hi buddy!"); |
| 23 | + alert("Cześć kolego!"); |
24 | 24 | }
|
25 | 25 | };
|
26 |
| - |
27 |
| -john.sayHi(); // Hi buddy! |
| 26 | + |
| 27 | +jan.sayHi(); // Cześć kolego! |
28 | 28 | ```
|
29 |
| - |
30 |
| -So here we've made an object `john` with the method `sayHi`. |
31 |
| - |
32 |
| -Many built-in objects already exist, such as those that work with dates, errors, HTML elements, etc. They have different properties and methods. |
33 |
| - |
34 |
| -But, these features come with a cost! |
35 |
| - |
36 |
| -Objects are "heavier" than primitives. They require additional resources to support the internal machinery. |
37 |
| - |
38 |
| -## A primitive as an object |
39 |
| - |
40 |
| -Here's the paradox faced by the creator of JavaScript: |
41 |
| - |
42 |
| -- There are many things one would want to do with a primitive like a string or a number. It would be great to access them as methods. |
43 |
| -- Primitives must be as fast and lightweight as possible. |
44 |
| - |
45 |
| -The solution looks a little bit awkward, but here it is: |
46 |
| - |
47 |
| -1. Primitives are still primitive. A single value, as desired. |
48 |
| -2. The language allows access to methods and properties of strings, numbers, booleans and symbols. |
49 |
| -3. In order for that to work, a special "object wrapper" that provides the extra functionality is created, and then is destroyed. |
50 |
| - |
51 |
| -The "object wrappers" are different for each primitive type and are called: `String`, `Number`, `Boolean` and `Symbol`. Thus, they provide different sets of methods. |
52 |
| - |
53 |
| -For instance, there exists a string method [str.toUpperCase()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase) that returns a capitalized `str`. |
54 |
| - |
55 |
| -Here's how it works: |
56 |
| - |
| 29 | + |
| 30 | +Stworzyliśmy tutaj obiekt `janek` z metodą `sayHi` |
| 31 | + |
| 32 | +Wiele wbudowanych obiektów już istnieje, na przykład takie jak te, które pracują z datami, błędami, elementami HTML, itd. Mają one różne własności i metody. |
| 33 | + |
| 34 | +Pamiętaj jednak, że te funkcje mają swój koszt! |
| 35 | + |
| 36 | +Obiekty są "cięższe" niż typy podstawowe. Wymagają dodatkowych zasobów żeby do obsługi wewnętrznych maszyn. |
| 37 | + |
| 38 | +## Typ podstawowy jest jak obiekt |
| 39 | + |
| 40 | + |
| 41 | +Oto paradoks, za którym którym stoi twórca JavaScript: |
| 42 | + |
| 43 | +- Jest wiele rzeczy, które chciałoby się zrobić z typami podstawowymi, takimi jak tekstowy lub liczba. Byłoby wspaniale uzyskać dostęp do nich jako metod. |
| 44 | +- Typy podstawowe są szybkie i muszą ważyć najmniej jak to możliwe. |
| 45 | + |
| 46 | +Rozwiązanie wygląda jest trochę dziwne. Oto ono: |
| 47 | + |
| 48 | +1. Typy podstawowe są wciąż typami podstawowymi. Pojedynczymi wartościami, tak jak zaprojektowano. |
| 49 | +2. Język pozwala dostać się do metod i własności typu tekstowego, liczbowego, logicznego i symboli. |
| 50 | +3. W tym celu stworzony został specjalny "wrapper obiektu", który dostarcza dodatkowych funkcji, a następnie jest on usuwany. |
| 51 | + |
| 52 | +"Wrappery obiektów" są różne dla każdego typu podstawowego i nazywają się: `String`, `Number`, `Boolean` i `Symbol`. W ten sposób zapewniają różne zestawy metod. |
| 53 | + |
| 54 | +Istnieje na przykład metoda typu tekstowego [str.toUpperCase()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase) która zwraca skapitalizowane `str`. |
| 55 | +Oto jak działa: |
57 | 56 | ```js run
|
58 |
| -let str = "Hello"; |
| 57 | +let str = "Cześć"; |
59 | 58 |
|
60 |
| -alert( str.toUpperCase() ); // HELLO |
| 59 | +alert( str.toUpperCase() ); // CZEŚĆ |
61 | 60 | ```
|
| 61 | +Proste, prawda ? Oto co dokładnie się dzieje wywołując `str.toUpperCase()`: |
62 | 62 |
|
63 |
| -Simple, right? Here's what actually happens in `str.toUpperCase()`: |
64 |
| - |
65 |
| -1. The string `str` is a primitive. So in the moment of accessing its property, a special object is created that knows the value of the string, and has useful methods, like `toUpperCase()`. |
66 |
| -2. That method runs and returns a new string (shown by `alert`). |
67 |
| -3. The special object is destroyed, leaving the primitive `str` alone. |
| 63 | +1. Ciąg znaków `str` jest typem podstawowym. W momencie gdy chcemy dostać się do jego własności, zostaje stworzony specjalny obiekt, który zna przypisaną do niego wartość i dostarcza przydatnych metod takich jak `toUpperCase()`. |
| 64 | +2. Metoda zostaje uruchomiona i zwraca nowy ciąg znaków (pokazany za pomocą `alert`). |
| 65 | +3. Nowo stworzony specjalny obiekt zostaje usunięty, pozostawiając sam typ podstawowy `str`. |
68 | 66 |
|
69 |
| -So primitives can provide methods, but they still remain lightweight. |
| 67 | +Tak więc typy podstawowe mogą dostarczać metod, ale wciąż pozostają lekkie. |
70 | 68 |
|
71 |
| -The JavaScript engine highly optimizes this process. It may even skip the creation of the extra object at all. But it must still adhere to the specification and behave as if it creates one. |
| 69 | +Silnik JavaScript silnie optymalizuje ten proces. Może nawet całkowicie pominąć tworzenie dodatkowego obiektu, ale wciąż ciąg znaków musi zachowywać się zgodne ze specyfikacją, tak jak gdyby został stworzony. |
72 | 70 |
|
73 |
| -A number has methods of its own, for instance, [toFixed(n)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed) rounds the number to the given precision: |
| 71 | +Typ liczbowy ma swoje własne metody, na przykład, [toFixed(n)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed) zaokrągla liczb do podanej precyzji: |
74 | 72 |
|
75 | 73 | ```js run
|
76 | 74 | let n = 1.23456;
|
77 | 75 |
|
78 | 76 | alert( n.toFixed(2) ); // 1.23
|
79 | 77 | ```
|
| 78 | +Więcej specyficznych metod poznamy w rozdziałach <info:number> i <info:string>. |
80 | 79 |
|
81 |
| -We'll see more specific methods in chapters <info:number> and <info:string>. |
82 |
| - |
| 80 | +````warn header="Konstruktory `String/Number/Boolean` są tylko do wewnętrznego odczytu" |
| 81 | +Niektóre języki programowania takie jak Java pozwalają nam na stworzenie "wrapperów obiektów" dla typów podstawowych używając następującej składni: `new Number(1)` albo `new Boolean(false)`. |
83 | 82 |
|
84 |
| -````warn header="Constructors `String/Number/Boolean` are for internal use only" |
85 |
| -Some languages like Java allow us to explicitly create "wrapper objects" for primitives using a syntax like `new Number(1)` or `new Boolean(false)`. |
| 83 | +W JavaScript także jest to możliwe z powodów historycznych, ale mocno **niezalecane**. W niektórych przypadkach powoduje to mocne niedopowiedzenia. |
86 | 84 |
|
87 |
| -In JavaScript, that's also possible for historical reasons, but highly **unrecommended**. Things will go crazy in several places. |
88 |
| - |
89 |
| -For instance: |
| 85 | +Na przykład: |
90 | 86 |
|
91 | 87 | ```js run
|
92 | 88 | alert( typeof 0 ); // "number"
|
93 | 89 |
|
94 | 90 | alert( typeof new Number(0) ); // "object"!
|
95 | 91 | ```
|
96 |
| - |
97 |
| -Objects are always truthy in `if`, so here the alert will show up: |
98 |
| - |
| 92 | +Obiekty są zawsze prawdziwe (ang. *truthy*) w `if`, więc ostrzeżenie zostanie wyświetlone: |
99 | 93 | ```js run
|
100 | 94 | let zero = new Number(0);
|
101 | 95 |
|
102 |
| -if (zero) { // zero is true, because it's an object |
103 |
| - alert( "zero is truthy!?!" ); |
| 96 | +if (zero) { // zero jest prawdziwe, ponieważ jest obiektem |
| 97 | + alert( "zero jest prawdziwe!?!" ); |
104 | 98 | }
|
105 | 99 | ```
|
106 | 100 |
|
107 |
| -On the other hand, using the same functions `String/Number/Boolean` without `new` is a totally sane and useful thing. They convert a value to the corresponding type: to a string, a number, or a boolean (primitive). |
| 101 | +Z drugiej strony, używając samych funkcji `String/Number/Boolean` bez operatora `new` jest całkowicie rozsądne i użyteczne. Zamieniają one wartości na odpowiadające im typy: typ tekstowy, liczbowy logiczny (typy podstawowe). |
108 | 102 |
|
109 |
| -For example, this is entirely valid: |
| 103 | +Na przykład jest to całkowicie poprawne: |
110 | 104 | ```js
|
111 |
| -let num = Number("123"); // convert a string to number |
| 105 | +let num = Number("123"); // zamienia ciąg znaków na liczbę |
112 | 106 | ```
|
113 | 107 | ````
|
114 | 108 |
|
115 | 109 |
|
116 |
| -````warn header="null/undefined have no methods" |
117 |
| -The special primitives `null` and `undefined` are exceptions. They have no corresponding "wrapper objects" and provide no methods. In a sense, they are "the most primitive". |
| 110 | +````warn header="null/undefined nie mają metod." |
| 111 | +Specjalne typy podstawowe `null` i `undefined` są wyjątkami. Nie mają odpowiadających im "wrapperów obiektów" i nie dostarczają metod. W pewnym sensie są najbardziej "podstawowe". |
118 | 112 |
|
119 |
| -An attempt to access a property of such value would give the error: |
| 113 | +Próba uzyskania dostępu do właściwości o takiej wartości spowodowałaby błąd: |
120 | 114 |
|
121 | 115 | ```js run
|
122 |
| -alert(null.test); // error |
| 116 | +alert(null.test); // błąd |
123 | 117 | ````
|
124 | 118 |
|
125 |
| -## Summary |
| 119 | +## Podsumowanie |
126 | 120 |
|
127 |
| -- Primitives except `null` and `undefined` provide many helpful methods. We will study those in the upcoming chapters. |
128 |
| -- Formally, these methods work via temporary objects, but JavaScript engines are well tuned to optimize that internally, so they are not expensive to call. |
| 121 | +- Typy podstawowe oprócz `null` i `undefined` dostarczają wielu przydatnych metod. Przestudiujemy te metody w następnych rozdziałach. |
| 122 | +- Formalnie metody te działają za pośrednictwem obiektów tymczasowych, ale silniki JavaScript są dobrze dostosowane i optymalizują to wewnętrznie, więc wywołanie ich nie jest drogie. |
0 commit comments