Skip to content

Commit 5e534a0

Browse files
authored
Merge pull request #52 from armino-dev/translate-object-methods-this
Object methods, "this"
2 parents fd1becf + b4fbec0 commit 5e534a0

File tree

10 files changed

+165
-169
lines changed

10 files changed

+165
-169
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
**Error**!
1+
**Eroare**!
22

3-
Try it:
3+
Incercați:
44

55
```js run
66
let user = {
77
name: "John",
88
go: function() { alert(this.name) }
99
}
1010

11-
(user.go)() // error!
11+
(user.go)() // eroare!
1212
```
1313

14-
The error message in most browsers does not give understanding what went wrong.
14+
Din mesajul de eroare furnizat de majoritatea browserelor nu reiese ce a mers prost.
1515

16-
**The error appears because a semicolon is missing after `user = {...}`.**
16+
**Eroarea apare deoarece simbolul ";" lipsește după `user = {...}`.**
1717

18-
JavaScript does not auto-insert a semicolon before a bracket `(user.go)()`, so it reads the code like:
18+
Limbajul JavaScript nu adaugă automat un simbol ";" înainte de paranteză `(user.go)()`, deci citește codul ca:
1919

2020
```js no-beautify
2121
let user = { go:... }(user.go)()
2222
```
2323

24-
Then we can also see that such a joint expression is syntactically a call of the object `{ go: ... }` as a function with the argument `(user.go)`. And that also happens on the same line with `let user`, so the `user` object has not yet even been defined, hence the error.
24+
Apoi, putem vedea, de asemenea, că o astfel de expresie combinată este sintactic un apel al obiectului `{ go: ... }` ca funcție cu argumentul `(user.go)`. Și asta se întâmplă și pe aceeași linie cu `let user`, deci obiectul `user` nu a fost încă definit, de unde și eroarea.
2525

26-
If we insert the semicolon, all is fine:
26+
Dacă introducem un simbol ";", totul este în regulă:
2727

2828
```js run
2929
let user = {
@@ -34,4 +34,4 @@ let user = {
3434
(user.go)() // John
3535
```
3636

37-
Please note that brackets around `(user.go)` do nothing here. Usually they setup the order of operations, but here the dot `.` works first anyway, so there's no effect. Only the semicolon thing matters.
37+
Rețineți că parantezele din jurul `(user.go)` nu fac nimic aici. De obicei setează ordinea operațiilor, dar aici punctul `.` are oricum precedență, deci nu are niciun efect. Singurul lucru care contează este simbolul ";".

Diff for: 1-js/04-object-basics/04-object-methods/2-check-syntax/task.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 2
22

33
---
44

5-
# Syntax check
5+
# Verificare de sintaxă
66

7-
What is the result of this code?
7+
Care este rezultatul următorului cod?
88

99

1010
```js no-beautify
@@ -16,4 +16,4 @@ let user = {
1616
(user.go)()
1717
```
1818

19-
P.S. There's a pitfall :)
19+
P.S. Există o capcană :)
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11

2-
Here's the explanations.
2+
Iată explicațiile.
33

4-
1. That's a regular object method call.
4+
1. Aceasta este o apelare normală a metodei obiectului.
55

6-
2. The same, brackets do not change the order of operations here, the dot is first anyway.
6+
2. Similar, aici, parantezele nu schimbă ordinea operațiilor, punctul este oricum primul.
77

8-
3. Here we have a more complex call `(expression).method()`. The call works as if it were split into two lines:
8+
3. Aici avem un apel mai complex `(expression).method()`. Apelul funcționează ca și cum ar fi împărțit în două linii:
99

1010
```js no-beautify
11-
f = obj.go; // calculate the expression
12-
f(); // call what we have
11+
f = obj.go; // calculează expresia
12+
f(); // apelează ce avem
1313
```
1414

15-
Here `f()` is executed as a function, without `this`.
15+
Aici `f()` este executat ca funcție, fără `this`.
1616

17-
4. The similar thing as `(3)`, to the left of the dot `.` we have an expression.
17+
4. Similar cu `(3)`, la stânga punctului `.` avem o expresie.
1818

19-
To explain the behavior of `(3)` and `(4)` we need to recall that property accessors (dot or square brackets) return a value of the Reference Type.
20-
21-
Any operation on it except a method call (like assignment `=` or `||`) turns it into an ordinary value, which does not carry the information allowing to set `this`.
19+
Pentru a explica comportamentul apelurilor `(3)` și `(4)` trebuie să ne reamintim că accesorii de proprietăți (punct sau paranteze pătrate) returnează o valoare de Tip Referință.
2220

21+
Orice operație pe aceasta, cu excepția unui apel de metodă (precum alocarea `=` sau `||`) o transformă într-o valoare obișnuită, care nu poartă informațiile ce permit setarea variabilei `this`.

Diff for: 1-js/04-object-basics/04-object-methods/3-why-this/task.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ importance: 3
22

33
---
44

5-
# Explain the value of "this"
5+
# Explicați valoarea variabilei "this"
66

7-
In the code below we intend to call `user.go()` method 4 times in a row.
7+
În codul următor intenționăm să apelăm metoda `user.go()` de 4 ori la rând.
88

9-
But calls `(1)` and `(2)` works differently from `(3)` and `(4)`. Why?
9+
Însă apelurile `(1)` și `(2)` funcționează diferit de `(3)` și `(4)`. De ce?
1010

1111
```js run no-beautify
1212
let obj, method;
@@ -23,4 +23,3 @@ obj.go(); // (1) [object Object]
2323

2424
(obj.go || obj.stop)(); // (4) undefined
2525
```
26-

Diff for: 1-js/04-object-basics/04-object-methods/4-object-property-this/solution.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
**Answer: an error.**
1+
**Răspuns: o eroare.**
22

3-
Try it:
3+
Încercați:
44
```js run
55
function makeUser() {
66
return {
@@ -11,18 +11,18 @@ function makeUser() {
1111

1212
let user = makeUser();
1313

14-
alert( user.ref.name ); // Error: Cannot read property 'name' of undefined
14+
alert( user.ref.name ); // Eroare: Cannot read property 'name' of undefined
1515
```
1616

17-
That's because rules that set `this` do not look at object definition. Only the moment of call matters.
17+
Acest lucru se datorează faptului că regulile care setează `this` nu se uită la definirea obiectului. Doar momentul apelului contează .
1818

19-
Here the value of `this` inside `makeUser()` is `undefined`, because it is called as a function, not as a method with "dot" syntax.
19+
Aici valoarea variabilei `this` din interiorul funcției `makeUser()` este `undefined`, deoarece este apelată ca funcție, nu ca o metodă cu sintaxa "punct".
2020

21-
The value of `this` is one for the whole function, code blocks and object literals do not affect it.
21+
Valoarea variabilei `this` este unică pentru întreaga funcția, blocurile de cod și obiectele literale nu o afectează.
2222

23-
So `ref: this` actually takes current `this` of the function.
23+
În concluzie `ref: this` preia de fapt variabila curentă `this` a funcției.
2424

25-
Here's the opposite case:
25+
Iată cazul opus:
2626

2727
```js run
2828
function makeUser() {
@@ -41,4 +41,4 @@ let user = makeUser();
4141
alert( user.ref().name ); // John
4242
```
4343

44-
Now it works, because `user.ref()` is a method. And the value of `this` is set to the object before dot `.`.
44+
Acum funcționează, pentru că `user.ref()` este o metodă. Iar valoarea variabilei `this` este setată la obiectul dinaintea punctului `.`.

Diff for: 1-js/04-object-basics/04-object-methods/4-object-property-this/task.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ importance: 5
22

33
---
44

5-
# Using "this" in object literal
5+
# Folosirea "this" în obiect literal
66

7-
Here the function `makeUser` returns an object.
7+
Aici funcția `makeUser` returnează un obiect.
88

9-
What is the result of accessing its `ref`? Why?
9+
Care este rezultatul accesării proprietății `ref`? De ce?
1010

1111
```js
1212
function makeUser() {
@@ -18,6 +18,5 @@ function makeUser() {
1818

1919
let user = makeUser();
2020

21-
alert( user.ref.name ); // What's the result?
21+
alert( user.ref.name ); // Care este rezultatul?
2222
```
23-

Diff for: 1-js/04-object-basics/04-object-methods/7-calculator/task.md

+6-7
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ importance: 5
22

33
---
44

5-
# Create a calculator
5+
# Creați un calculator
66

7-
Create an object `calculator` with three methods:
7+
Creați un obiect `calculator` cu trei metode:
88

9-
- `read()` prompts for two values and saves them as object properties.
10-
- `sum()` returns the sum of saved values.
11-
- `mul()` multiplies saved values and returns the result.
9+
- `read()` solicită două valori și le salvează ca proprietăți ale obiectului.
10+
- `sum()` returnează suma valorilor salvate.
11+
- `mul()` multiplică valorile salvate și returnează rezultatul.
1212

1313
```js
1414
let calculator = {
15-
// ... your code ...
15+
// ... codul vostru ...
1616
};
1717

1818
calculator.read();
@@ -21,4 +21,3 @@ alert( calculator.mul() );
2121
```
2222

2323
[demo]
24-

Diff for: 1-js/04-object-basics/04-object-methods/8-chain-calls/solution.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The solution is to return the object itself from every call.
1+
Soluția este de a returna obiectul însuși la fiecare apelare.
22

33
```js run demo
44
let ladder = {
@@ -26,7 +26,7 @@ let ladder = {
2626
ladder.up().up().down().up().down().showStep(); // 1
2727
```
2828

29-
We also can write a single call per line. For long chains it's more readable:
29+
De asemenea, putem scrie câte o apelare pe linie. Pentru înlănțuirile lungi este mai lizibil:
3030

3131
```js
3232
ladder

Diff for: 1-js/04-object-basics/04-object-methods/8-chain-calls/task.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@ importance: 2
22

33
---
44

5-
# Chaining
5+
# Înlănțuirea
66

7-
There's a `ladder` object that allows to go up and down:
7+
Există un obiect `ladder` (scară) care permite urcarea și coborârea:
88

99
```js
1010
let ladder = {
1111
step: 0,
12-
up() {
12+
up() {
1313
this.step++;
1414
},
15-
down() {
15+
down() {
1616
this.step--;
1717
},
18-
showStep: function() { // shows the current step
18+
showStep: function() { // arată pasul curent
1919
alert( this.step );
2020
}
2121
};
2222
```
2323

24-
Now, if we need to make several calls in sequence, can do it like this:
24+
În acest moment, dacă avem nevoie să facem mai multe apelări în ordine, le putem face astfel:
2525

2626
```js
2727
ladder.up();
@@ -30,10 +30,10 @@ ladder.down();
3030
ladder.showStep(); // 1
3131
```
3232

33-
Modify the code of `up`, `down` and `showStep` to make the calls chainable, like this:
33+
Modificați codul pentru `up`, `down` și `showStep` pentru a face apelările înlănțuibile, astfel:
3434

3535
```js
3636
ladder.up().up().down().showStep(); // 1
3737
```
3838

39-
Such approach is widely used across JavaScript libraries.
39+
Asemenea abordare este utilizată pe scară largă în bibliotecile JavaScript.

0 commit comments

Comments
 (0)