You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/04-object-basics/09-object-toprimitive/article.md
+38-38Lines changed: 38 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,22 +5,22 @@ Ce se întâmplă atunci când obiectele sunt adunate `obj1 + obj2`, scăzute `o
5
5
6
6
JavaScript nu prea permite să personalizeze cum operatorii lucrează pe obiecte. Spre deosebire de alte limbaje de programare, cum ar fi Ruby sau C++, nu putem implementa o metodă specială de obiect pentru a gestiona o adunare (sau alți operatori).
7
7
8
-
In case of such operations, objects are auto-converted to primitives, and then the operation is carried out over these primitives and results in a primitive value.
8
+
În cazul unor astfel de operații, obiectele sunt convertite automat în primitive, iar apoi operația este efectuată asupra acestor primitive și are ca rezultat o valoare primitivă.
9
9
10
-
That's an important limitation, as the result of `obj1 + obj2`can't be another object!
10
+
Aceasta este o limitare importantă, deoarece rezultatul a `obj1 + obj2`nu poate fi un alt obiect!
11
11
12
-
E.g. we can't make objects representing vectors or matrices (or achievements or whatever), add them and expect a "summed" object as the result. Such architectural feats are automatically "off the board".
12
+
E.g., nu putem crea obiecte reprezentând vectori sau matrici (sau realizări sau orice altceva), să le adunăm și să ne așteptăm ca rezultat un obiect "însumat". Astfel de realizări arhitecturale sunt automat "în afara discuției".
13
13
14
-
So, because we can't do much here, there's no maths with objects in real projects. When it happens, it's usually because of a coding mistake.
14
+
Așadar, pentru că nu putem face mare lucru aici, nu există matematică cu obiecte în proiectele reale. Atunci când se întâmplă, de obicei, este din cauza unei greșeli de codare.
15
15
16
-
In this chapter we'll cover how an object converts to primitive and how to customize it.
16
+
În acest capitol vom acoperi modul în care un obiect se convertește în primitivă și cum să îl personalizăm.
17
17
18
-
We have two purposes:
18
+
Avem două scopuri:
19
19
20
-
1.It will allow us to understand what's going on in case of coding mistakes, when such an operation happened accidentally.
21
-
2.There are exceptions, where such operations are possible and look good. E.g. subtracting or comparing dates (`Date` objects). We'll come across them later.
20
+
1.Ne va permite să înțelegem ce se întâmplă în cazul unor greșeli de codare, atunci când o astfel de operațiune s-a întâmplat accidental.
21
+
2.Există excepții, unde astfel de operații sunt posibile și arată bine. E.g. scăderea sau compararea datelor (obiecte `Date`). Le vom întâlni mai târziu.
22
22
23
-
## Conversion rules
23
+
## Reguli de conversie
24
24
25
25
În capitolul <info: tip-conversions> am văzut regulile pentru conversiile numerice, string și booleane ale primitivelor. Dar am lăsat un gol pentru obiecte. Acum, după cum știm despre metode și simboluri, devine posibil să o completăm.
26
26
@@ -99,15 +99,15 @@ Să începem de la prima metodă. Există un simbol încorporat numit `Symbol.to
99
99
100
100
```js
101
101
obj[Symbol.toPrimitive] = function(hint) {
102
-
// here goes the code to convert this object to a primitive
102
+
// iată codul pentru a converti acest obiect într-o primitivă
103
103
// trebuie să returneze o valoare primitivă
104
104
// hint = unul dintre "string", "number", "default"
105
105
};
106
106
```
107
107
108
-
If the method`Symbol.toPrimitive` exists, it's used for all hints, and no more methods are needed.
108
+
Dacă există metoda`Symbol.toPrimitive`, aceasta este utilizată pentru toate indiciile și nu mai sunt necesare alte metode.
109
109
110
-
For instance, here `user`object implements it:
110
+
De exemplu, aici obiectul `user`o implementează:
111
111
112
112
```js run
113
113
let user = {
@@ -131,21 +131,21 @@ După cum putem vedea din cod, obiectul `user` devine un șir autodescriptiv sau
131
131
132
132
## toString/valueOf
133
133
134
-
If there's no`Symbol.toPrimitive` then JavaScript tries to find methods`toString`and`valueOf`:
134
+
Dacă nu există`Symbol.toPrimitive`, atunci JavaScript încearcă să găsească metodele`toString`și`valueOf`:
135
135
136
-
-For the "string" hint: `toString`, and if it doesn't exist, then`valueOf` (so`toString`has the priority for string conversions).
137
-
-For other hints: `valueOf`, and if it doesn't exist, then`toString` (so `valueOf`has the priority for maths).
136
+
-Pentru indiciul "string": `toString`, iar dacă nu există, atunci`valueOf` (deci`toString`are prioritate pentru conversiile de stringuri).
137
+
-Pentru alte indicii: `valueOf`, iar dacă nu există, atunci`toString` (astfel încât `valueOf`are prioritate pentru conversii matematice).
138
138
139
-
Methods`toString`and`valueOf`come from ancient times. They are not symbols (symbols did not exist that long ago), but rather "regular" string-named methods. They provide an alternative "old-style" way to implement the conversion.
139
+
Metodele`toString`și`valueOf`provin din timpuri străvechi. Ele nu sunt simboluri (simbolurile nu existau cu mult timp în urmă), ci mai degrabă metode "obișnuite" cu nume de șir de caractere. Ele oferă o modalitate alternativă "în stil vechi" de a implementa conversia.
140
140
141
-
These methods must return a primitive value. If`toString`or`valueOf`returns an object, then it's ignored (same as if there were no method).
141
+
Aceste metode trebuie să returneze o valoare primitivă. Dacă`toString`sau`valueOf`returnează un obiect, atunci este ignorată (la fel ca în cazul în care nu ar exista nicio metodă).
142
142
143
-
By default, a plain object has following `toString`and`valueOf` methods:
143
+
În mod implicit, un obiect simplu are următoarele metode `toString`și`valueOf`:
144
144
145
-
-The`toString`method returns a string `"[object Object]"`.
146
-
-The`valueOf`method returns the object itself.
145
+
-Metoda`toString`returnează un șir de caractere `"[object Object Object]"`.
So if we try to use an object as a string, like in an `alert`or so, then by default we see`[object Object]`.
157
+
Așadar, dacă încercăm să folosim un obiect ca șir de caractere, cum ar fi într-un `alert`sau așa ceva, atunci, în mod implicit, vom vedea`[object Object]`.
158
158
159
-
The default`valueOf`is mentioned here only for the sake of completeness, to avoid any confusion. As you can see, it returns the object itself, and so is ignored. Don't ask me why, that's for historical reasons. So we can assume it doesn't exist.
159
+
Valoarea implicită`valueOf`este menționată aici doar de dragul completării, pentru a evita orice confuzie. După cum puteți vedea, acesta returnează obiectul în sine, deci este ignorat. Nu mă întrebați de ce, aceea este din motive istorice. Deci putem presupune că nu există.
160
160
161
-
Let's implement these methods to customize the conversion.
161
+
Haideți să implementăm aceste metode pentru a personaliza conversia.
162
162
163
-
For instance, here`user`does the same as above using a combination of `toString`and`valueOf`instead of`Symbol.toPrimitive`:
163
+
De exemplu, aici`user`face același lucru ca mai sus, folosind o combinație de `toString`și`valueOf`în loc de`Symbol.toPrimitive`:
164
164
165
165
```js run
166
166
let user = {
167
167
name:"John",
168
-
money:1000,
168
+
bani:1000,
169
169
170
170
// pentru indiciu="string"
171
171
toString() {
172
-
return`{name: "${this.name}"}`;
172
+
return`{nume: "${acest.nume}"}`;
173
173
},
174
174
175
175
// pentru indiciu="number" sau "default"
@@ -217,31 +217,31 @@ Din motive istorice, dacă `toString` sau `valueOf` returnează un obiect, nu es
217
217
În contrast, `Symbol.toPrimitive` *trebuie* să returneze o primitivă, altfel va exista o eroare.
218
218
```
219
219
220
-
## Further conversions
220
+
## Alte conversii
221
221
222
-
As we know already, many operators and functions perform type conversions, e.g. multiplication`*`converts operands to numbers.
222
+
După cum știm deja, mulți operatori și funcții efectuează conversii de tip, e.g. înmulțirea`*`convertește operanzii în numere.
223
223
224
-
If we pass an object as an argument, then there are two stages:
225
-
1.The object is converted to a primitive (using the rules described above).
226
-
2.If the resulting primitive isn't of the right type, it's converted.
224
+
Dacă trecem un obiect ca argument, atunci există două etape:
225
+
1.Obiectul este convertit într-o primitivă (folosind regulile descrise mai sus).
226
+
2.Dacă primitiva rezultată nu este de tipul corect, aceasta este convertită.
227
227
228
228
De exemplu:
229
229
230
230
```js run
231
231
let obj = {
232
-
// toString handles all conversions in the absence of other methods
232
+
// toString se ocupă de toate conversiile în absența altor metode
233
233
toString() {
234
234
return"2";
235
235
}
236
236
};
237
237
238
-
alert(obj *2); // 4, object converted to primitive "2", then multiplication made it a number
238
+
alert(obj *2); // 4, obiect convertit în primitivul "2", apoi înmulțirea l-a transformat în număr
239
239
```
240
240
241
-
1.The multiplication `obj * 2`first converts the object to primitive (that's a string`"2"`).
242
-
2.Then`"2" * 2`becomes`2 * 2` (the string is converted to number).
241
+
1.Înmulțirea `obj * 2`convertește mai întâi obiectul în primitivă (care este un șir de caractere`"2"`).
242
+
2.Apoi`"2" * 2`devine`2 * 2` (șirul este convertit în număr).
243
243
244
-
Binary plus will concatenate strings in the same situation, as it gladly accepts a string:
244
+
Binary plus va concatena șiruri de caractere în aceeași situație, deoarece acceptă cu plăcere un șir de caractere:
245
245
246
246
```js run
247
247
let obj = {
@@ -250,7 +250,7 @@ let obj = {
250
250
}
251
251
};
252
252
253
-
alert(obj +2); // 22 ("2" + 2), conversion to primitive returned a string => concatenation
253
+
alert(obj +2); // 22 ("2" + 2), conversia în primitivă a returnat un șir => concatenare
0 commit comments