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/01-object/article.md
+80-80Lines changed: 80 additions & 80 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,109 +1,109 @@
1
1
2
-
# Objects
2
+
# Obiecte
3
3
4
-
As we know from the chapter <info:types>, there are seven data types in JavaScript. Six of them are called "primitive", because their values contain only a single thing (be it a string or a number or whatever).
4
+
Așa cum știm din capitolul <info:types>, în JavaScript există șapte tipuri de date. Șase dintre ele sunt denumite "primitive", pentru că ele conțin numai un singur lucru (fie un string, un număr sau altceva).
5
5
6
-
In contrast, objects are used to store keyed collections of various data and more complex entities. In JavaScript, objects penetrate almost every aspect of the language. So we must understand them first before going in-depth anywhere else.
6
+
În contrast, obiectele sunt folosite pentru a stoca colecții indexate de date diferite si alte entități complexe. În JavaScript, obiectele pătrund în aproape toate aspectele limbajului. De aceea trebuie să ințelegem obiectele înainte de a intra mai adânc în altă parte.
7
7
8
-
An object can be created with figure brackets `{…}`with an optional list of *properties*. A property is a "key: value" pair, where `key` is a string (also called a "property name"), and `value` can be anything.
8
+
Un obiect poate fi creat folosind acoladele `{…}`cu o listă opțională de *proprietăți*. O proprietate este o pereche "cheie: valoare", unde `cheie` este un string (denumit si "numele proprietății"), iar `valoare` poate fi orice.
9
9
10
-
We can imagine an object as a cabinet with signed files. Every piece of data is stored in its file by the key. It's easy to find a file by its name or add/remove a file.
10
+
Ne putem imagina un obiect ca fiind un dosar ce conține documente semnate. Fiecare bucățică de informație este stocată în dosarul ei după `cheie`. Este ușor să găsești un document după nume sau să adaugi/îndepărtezi un document.
11
11
12
12

13
13
14
-
An empty object ("empty cabinet") can be created using one of two syntaxes:
14
+
Un obiect gol ("dosar gol") poate fi creat folosind una dintre următoarele sintaxe:
15
15
16
16
```js
17
-
let user =newObject(); //"object constructor" syntax
18
-
let user = {}; //"object literal" syntax
17
+
let user =newObject(); //sintaxa "constructor obiect"
18
+
let user = {}; //sintaxa "obiect literal"
19
19
```
20
20
21
21

22
22
23
-
Usually, the figure brackets`{...}` are used. That declaration is called an *object literal*.
23
+
Uzual se folosesc acoladele`{...}`. Această declarație este denumită *obiect literal*.
24
24
25
-
## Literals and properties
25
+
## Literalele și proprietățile
26
26
27
-
We can immediately put some properties into `{...}`as "key: value" pairs:
27
+
Putem pune imediat câteva proprietăți în `{...}`ca și perechi "cheie: valoare":
A property has a key (also known as "name" or "identifier") before the colon `":"`and a value to the right of it.
36
+
O proprietate are o cheie (denumită și "nume" sau "identificator") înainte de semnul două puncte `":"`și o valoare la dreapta semnului.
37
37
38
-
In the`user` object, there are two properties:
38
+
În obiectul`user`, există două proprietăți:
39
39
40
-
1.The first property has the name `"name"`and the value`"John"`.
41
-
2.The second one has the name `"age"`and the value`30`.
40
+
1.Prima proprietate are numele `"name"`și valoarea`"John"`.
41
+
2.A doua are numele `"age"`și valoarea`30`.
42
42
43
-
The resulting`user`object can be imagined as a cabinet with two signed files labeled "name" and "age".
43
+
Obiectul rezultat`user`poate fi imaginat ca un dosar cu două documente marcate cu "name" respectiv "age".
44
44
45
45

46
46
47
-
We can add, remove and read files from it any time.
47
+
Putem adăuga, șterge sau citi documente din dosar la orice moment.
48
48
49
-
Property values are accessible using the dot notation:
49
+
Valorile proprietăților sunt accesibile folosind notația cu punct.
50
50
51
51
```js
52
-
//get property values of the object:
52
+
//citește valorile proprietăților obiectului:
53
53
alert( user.name ); // John
54
54
alert( user.age ); // 30
55
55
```
56
56
57
-
The value can be of any type. Let's add a boolean one:
57
+
Valoarea poate avea orice tip. Să adăugăm una de tip `boolean`:
58
58
59
59
```js
60
60
user.isAdmin=true;
61
61
```
62
62
63
63

64
64
65
-
To remove a property, we can use`delete` operator:
65
+
Pentru a șterge o proprietate, putem folosi operatorul`delete`:
66
66
67
67
```js
68
68
deleteuser.age;
69
69
```
70
70
71
71

72
72
73
-
We can also use multiword property names, but then they must be quoted:
73
+
Putem folosi de asemenea mai multe cuvinte ca și nume ale proprietăților, dar acestea trebuie sa fie între ghilimele:
74
74
75
75
```js
76
76
let user = {
77
77
name:"John",
78
78
age:30,
79
-
"likes birds":true//multiword property name must be quoted
79
+
"likes birds":true//numele proprietăților formate din mai multe cuvinte trebuie să fie între ghilimele
80
80
};
81
81
```
82
82
83
83

84
84
85
85
86
-
The last property in the list may end with a comma:
86
+
Ultima proprietate din listă se poate termina cu virgulă:
87
87
```js
88
88
let user = {
89
89
name:"John",
90
90
age:30*!*,*/!*
91
91
}
92
92
```
93
-
That is called a "trailing" or "hanging" comma. Makes it easier to add/remove/move around properties, because all lines become alike.
93
+
Aceasta se numește virgulă "de sfârșit" sau "agățatoare". In felul acesta este mai ușor de adăugat/șters/mutat în jurul proprietăților, deoarece toate liniile vor fi asemănătoare.
94
94
95
-
## Square brackets
95
+
## Paranteze pătrate
96
96
97
-
For multiword properties, the dot access doesn't work:
97
+
Accesul la proprietățile formate din mai multe cuvinte nu se poate face cu punct:
98
98
99
99
```js run
100
-
//this would give a syntax error
100
+
//aceasta va genera o erroare de sintaxă
101
101
user.likes birds =true
102
102
```
103
103
104
-
That's because the dot requires the key to be a valid variable identifier. That is: no spaces and other limitations.
104
+
Asta deorece cheia trebuie să fie un identificator valid pentru o variabilă, adică: fără spații și alte limitări.
105
105
106
-
There's an alternative "square bracket notation" that works with any string:
106
+
Există ca alternativă "notația parantezelor pătrate" care funcționează cu orice fel de șir de caractere:
Now everything is fine. Please note that the string inside the brackets is properly quoted (any type of quotes will do).
121
+
Acum totul este în regulă. De remarcat că șirul de caractere este citat corespunzător (oricare dintre semnele pentru ghilimele se accepta).
122
122
123
-
Square brackets also provide a way to obtain the property name as the result of any expression -- as opposed to a literal string -- like from a variable as follows:
123
+
Cu parantezele pătrate se poate obține numele proprietății ca rezultat al unei expresii -- as opposed to a literal string -- dintr-o variabilă:
124
124
125
125
```js
126
126
let key ="likes birds";
127
127
128
-
//same as user["likes birds"] = true;
128
+
//la fel ca user["likes birds"] = true;
129
129
user[key] =true;
130
130
```
131
131
132
-
Here, the variable `key`may be calculated at run-time or depend on the user input. And then we use it to access the property. That gives us a great deal of flexibility.
132
+
Aici, variabila `key`poate fi calculată la timpul rulării sau poate depinde de datele introduse de utilizatori. Dupa aceea o putem folosi pentru a accesa proprietatea. Acest lucru ne oferă o flexibilitate mărită.
133
133
134
-
For instance:
134
+
De exemplu:
135
135
136
136
```js run
137
137
let user = {
@@ -145,7 +145,7 @@ let key = prompt("What do you want to know about the user?", "name");
145
145
alert( user[key] ); // John (if enter "name")
146
146
```
147
147
148
-
The dot notation cannot be used in a similar way:
148
+
Notația cu punct nu poate fi folosită în același mod:
149
149
150
150
```js run
151
151
let user = {
@@ -157,40 +157,40 @@ let key = "name";
157
157
alert( user.key ) // undefined
158
158
```
159
159
160
-
### Computed properties
160
+
### Proprietăți calculate
161
161
162
-
We can use square brackets in an object literal. That's called *computed properties*.
162
+
Putem folosi parantezele pătrate intr-un obiect literal. Asta se numește *proprietate calculată*.
163
163
164
-
For instance:
164
+
De exemplu:
165
165
166
166
```js run
167
167
let fruit =prompt("Which fruit to buy?", "apple");
168
168
169
169
let bag = {
170
170
*!*
171
-
[fruit]:5, //the name of the property is taken from the variable fruit
171
+
[fruit]:5, //numele proprietații este luat din variabila fruit
172
172
*/!*
173
173
};
174
174
175
175
alert( bag.apple ); // 5 if fruit="apple"
176
176
```
177
177
178
-
The meaning of a computed property is simple: `[fruit]`means that the property name should be taken from`fruit`.
178
+
Semnificația unei proprietăți calculate este simplă: `[fruit]`inseamnă că numele proprietății trebuie obținut din`fruit`.
179
179
180
-
So, if a visitor enters`"apple"`, `bag`will become`{apple: 5}`.
180
+
Deci, daca un vizitator introduce`"apple"`, `bag`va deveni`{apple: 5}`.
181
181
182
-
Essentially, that works the same as:
182
+
În principiu, are aceeași funcționalitate ca și:
183
183
```js run
184
184
let fruit =prompt("Which fruit to buy?", "apple");
185
185
let bag = {};
186
186
187
-
//take property name from the fruit variable
187
+
//obține numele proprietății din variabila fruit
188
188
bag[fruit] =5;
189
189
```
190
190
191
-
...But looks nicer.
191
+
...Dar arată mai bine.
192
192
193
-
We can use more complex expressions inside square brackets:
193
+
Putem folosi expresii mai complexe între parantezele pătrate:
194
194
195
195
```js
196
196
let fruit ='apple';
@@ -199,16 +199,16 @@ let bag = {
199
199
};
200
200
```
201
201
202
-
Square brackets are much more powerful than the dot notation. They allow any property names and variables. But they are also more cumbersome to write.
202
+
Parantezele pătrate sunt mult mai puternice decât notația cu punct. Ele permit orice fel de nume și de variabile pentru proprietăți, dar sunt mai dificil de scris.
203
203
204
-
So most of the time, when property names are known and simple, the dot is used. And if we need something more complex, then we switch to square brackets.
204
+
Deci, de cele mai multe ori, când numele proprietăților sunt cunoscute și simple se folosește punctul, iar daca avem nevoie de ceva mai complex, trecem la parantezele pătrate.
205
205
206
206
207
207
208
-
````smart header="Reserved words are allowed as property names"
209
-
A variable cannot have a name equal to one of language-reserved words like "for", "let", "return" etc.
208
+
````smart header="Cuvintele rezervate sunt permise ca nume de proprietăți"
209
+
O variabilă nu poate avea un nume identic cu unul dintre cuvintele rezervate limbajului precum "for", "let", "return" etc.
210
210
211
-
But for an object property, there's no such restriction. Any name is fine:
211
+
În schimb, pentru o proprietate a unui obiect nu există o astfel de restricție. Orice nume este bun:
212
212
213
213
```js run
214
214
let obj = {
@@ -220,31 +220,31 @@ let obj = {
220
220
alert( obj.for + obj.let + obj.return ); // 6
221
221
```
222
222
223
-
Basically, any name is allowed, but there's a special one: `"__proto__"` that gets special treatment for historical reasons. For instance, we can't set it to a non-object value:
223
+
Practic, orice nume este permis, însă există unul special: `"__proto__"` care primește un tratament preferențial din motive istorice. De exemplu, nu îl putem seta la o valoare non-obiect:
224
224
225
225
```js run
226
226
let obj = {};
227
227
obj.__proto__ = 5;
228
-
alert(obj.__proto__); // [object Object], didn't work as intended
228
+
alert(obj.__proto__); // [object Object], nu a funcționat cum ne-am așteptat
229
229
```
230
230
231
-
As we see from the code, the assignment to a primitive `5` is ignored.
231
+
După cum vedem din cod, atribuirea către o primitivă `5` este ignorată.
232
232
233
-
That can become a source of bugs and even vulnerabilities if we intend to store arbitrary key-value pairs in an object, and allow a visitor to specify the keys.
233
+
Acest lucru poate deveni o sursă de bug-uri și chiar vulnerabilități dacă intenționăm să stocăm perechi cheie-valoare arbitrare într-un obiect și să permitem unui vizitator să specifice cheile.
234
234
235
-
In that case the visitor may choose `__proto__` as the key, and the assignment logic will be ruined (as shown above).
235
+
În acest caz vizitatorul poate alege `__proto__` ca și cheie, iar logica de atribuire va fi stricată (după cum se arată mai sus).
236
236
237
-
There is a way to make objects treat `__proto__` as a regular property, which we'll cover later, but first we need to know more about objects.
237
+
Există o modalitate, pe care o vom acoperi mai târziu, de a face obiectele să trateze `__proto__` ca pe o proprietate obișnuită, dar mai întâi trebuie să știm mai multe despre obiecte.
238
238
239
-
There's also another data structure [Map](info:map-set), that we'll learn in the chapter <info:map-set>, which supports arbitrary keys.
239
+
Există, de asemenea, o altă structură de date [Map](info:map-set), pe care o vom învăța în capitolul <info:map-set>, care acceptă chei arbitrare.
240
240
````
241
241
242
242
243
-
## Property value shorthand
243
+
## Prescurtare (shorthand) pentru valoarea proprietății
244
244
245
-
In real code we often use existing variables as values for property names.
245
+
În cod real folosim des variabile existente ca valori pentru numele proprietăților.
246
246
247
-
For instance:
247
+
De exemplu:
248
248
249
249
```js run
250
250
functionmakeUser(name, age) {
@@ -259,60 +259,60 @@ let user = makeUser("John", 30);
259
259
alert(user.name); // John
260
260
```
261
261
262
-
In the example above, properties have the same names as variables. The use-case of making a property from a variable is so common, that there's a special *property value shorthand* to make it shorter.
262
+
În exemplul de mai sus, proprietățile au aceleași nume ca variabilele. Cazul de utilizare al creării unei proprietăți dintr-o variabilă este atât de comun, încât există un *shorthand al valorii proprietății* pentru a o face mai scurtă.
263
263
264
-
Instead of `name:name`we can just write `name`, like this:
264
+
In loc de `name:name`putem scrie doar `name`, după cum urmeză:
265
265
266
266
```js
267
267
functionmakeUser(name, age) {
268
268
*!*
269
269
return {
270
-
name, //same as name: name
271
-
age //same as age: age
270
+
name, //la fel ca name: name
271
+
age //la fel ca age: age
272
272
// ...
273
273
};
274
274
*/!*
275
275
}
276
276
```
277
277
278
-
We can use both normal properties and shorthands in the same object:
278
+
Putem folosi atât proprietăți normale, cât și shorthand-uri în același obiect:
279
279
280
280
```js
281
281
let user = {
282
-
name, //same as name:name
282
+
name, //la fel ca name:name
283
283
age:30
284
284
};
285
285
```
286
286
287
-
## Existence check
287
+
## Verificarea existenței
288
288
289
-
A notable objects feature is that it's possible to access any property. There will be no error if the property doesn't exist! Accessing a non-existing property just returns`undefined`. It provides a very common way to test whether the property exists -- to get it and compare vs undefined:
289
+
O caracteristică notabilă a obiectelor este posibilitatea accesării oricărei proprietăți. Nu va fi nicio eroare dacă proprietatea nu există! Accesarea unei proprietăți inexistente doar returnează`undefined`. Oferă un mod foarte comun de a testa dacă proprietatea există -- de a o obține și a o compara cu undefined:
290
290
291
291
```js run
292
292
let user = {};
293
293
294
-
alert( user.noSuchProperty===undefined ); // true means "no such property"
294
+
alert( user.noSuchProperty===undefined ); // true înseamna "nu există proprietatea"
295
295
```
296
296
297
-
There also exists a special operator `"in"`to check for the existence of a property.
297
+
Există, de asemenea, un operator special `"in"`pentru a verifica existența unei proprietăți.
298
298
299
-
The syntax is:
299
+
Sintaxa este:
300
300
```js
301
301
"key"in object
302
302
```
303
303
304
-
For instance:
304
+
De exemplu:
305
305
306
306
```js run
307
307
let user = { name:"John", age:30 };
308
308
309
-
alert( "age"in user ); // true, user.age exists
310
-
alert( "blabla"in user ); // false, user.blabla doesn't exist
309
+
alert( "age"in user ); // true, user.age există
310
+
alert( "blabla"in user ); // false, user.blabla nu există
311
311
```
312
312
313
-
Please note that on the left side of `in`there must be a *property name*. That's usually a quoted string.
313
+
Luați aminte că, la stânga operatorului `in`trebuie sa fie un *nume de proprietate*. Usual este un sir caractere între ghilimele.
314
314
315
-
If we omit quotes, that would mean a variable containing the actual name will be tested. For instance:
315
+
Daca omitem ghilimelele, asta ar însemna că o variabilă care conține de fapt numele va fi testată. De exemplu:
0 commit comments