|
1 | | -# Code structure |
| 1 | +# Structura codului |
2 | 2 |
|
3 | | -The first thing we'll study is the building blocks of code. |
| 3 | +Primele lucruri pe care le vom studia sunt blocurile de bază ale codului. |
4 | 4 |
|
5 | | -## Statements |
| 5 | +## Declarații |
6 | 6 |
|
7 | | -Statements are syntax constructs and commands that perform actions. |
| 7 | +Declarațiile sunt construcții de sintaxă și comenzi care efectuează acțiuni. |
8 | 8 |
|
9 | | -We've already seen a statement, `alert('Hello, world!')`, which shows the message "Hello, world!". |
| 9 | +Deja am întâlnit o declarație, `alert('Hello, world!')`, care afișează mesajul "Hello, world!". |
10 | 10 |
|
11 | | -We can have as many statements in our code as we want. Statements can be separated with a semicolon. |
| 11 | +Putem avea cât de multe declarații vrem în codul nostru. Declarațiile pot fi separate prin punct și virgulă. |
12 | 12 |
|
13 | | -For example, here we split "Hello World" into two alerts: |
| 13 | +De exemplu, aici vom despărți "Hello World" în două alerte diferite: |
14 | 14 |
|
15 | 15 | ```js run no-beautify |
16 | 16 | alert('Hello'); alert('World'); |
17 | 17 | ``` |
18 | 18 |
|
19 | | -Usually, statements are written on separate lines to make the code more readable: |
| 19 | +De obicei, declarațiile sunt scrise pe linii separate pentru a face codul mai lizibil: |
20 | 20 |
|
21 | 21 | ```js run no-beautify |
22 | 22 | alert('Hello'); |
23 | 23 | alert('World'); |
24 | 24 | ``` |
25 | 25 |
|
26 | | -## Semicolons [#semicolon] |
| 26 | +## Punctul și virgula [#semicolon] |
27 | 27 |
|
28 | | -A semicolon may be omitted in most cases when a line break exists. |
| 28 | +Punctul și virgula pot fi omise în majoritatea cazurilor când există o pauză de linie. |
29 | 29 |
|
30 | | -This would also work: |
| 30 | +Acest lucru ar funcționa, de asemenea: |
31 | 31 |
|
32 | 32 | ```js run no-beautify |
33 | 33 | alert('Hello') |
34 | 34 | alert('World') |
35 | 35 | ``` |
36 | 36 |
|
37 | | -Here, JavaScript interprets the line break as an "implicit" semicolon. This is called an [automatic semicolon insertion](https://tc39.github.io/ecma262/#sec-automatic-semicolon-insertion). |
| 37 | +Aici, JavaScript interpretează întreruperea liniei ca punct și virgulă „implicit”. Aceasta se numește [introducerea automata a punctului și virgulei](https://tc39.github.io/ecma262/#sec-automatic-semicolon-insertion). |
38 | 38 |
|
39 | | -**In most cases, a newline implies a semicolon. But "in most cases" does not mean "always"!** |
| 39 | +**În cele mai multe cazuri, o linie nouă implică punct și virgulă. Dar „în majoritatea cazurilor” nu înseamnă „întotdeauna!** |
40 | 40 |
|
41 | | -There are cases when a newline does not mean a semicolon. For example: |
| 41 | +Sunt cazuri când o linie nouă nu înseamna un punct și virgulă. De exemplu: |
42 | 42 |
|
43 | 43 | ```js run no-beautify |
44 | 44 | alert(3 + |
45 | 45 | 1 |
46 | 46 | + 2); |
47 | 47 | ``` |
48 | 48 |
|
49 | | -The code outputs `6` because JavaScript does not insert semicolons here. It is intuitively obvious that if the line ends with a plus `"+"`, then it is an "incomplete expression", so the semicolon is not required. And in this case that works as intended. |
| 49 | +Codul va afișa `6` deoarece JavaScript nu inserează punct și virgulă aici. Este intuitiv evident că, dacă linia se termină cu un plus `"+"`, atunci este o "expresie incompletă", deci punctul si virgula nu este necesar. Și în acest caz, funcționează așa cum este intenționat |
50 | 50 |
|
51 | | -**But there are situations where JavaScript "fails" to assume a semicolon where it is really needed.** |
| 51 | +**Există situații în care JavaScript „eșuează” să-și asume un punct și virgulă unde este cu adevărat necesar.** |
52 | 52 |
|
53 | | -Errors which occur in such cases are quite hard to find and fix. |
| 53 | +Erorile care apar în astfel de cazuri sunt destul de greu de găsit și de remediat. |
54 | 54 |
|
55 | | -````smart header="An example of an error" |
56 | | -If you're curious to see a concrete example of such an error, check this code out: |
| 55 | +````smart header="Exemplu de eroare" |
| 56 | +Dacă sunteți curios să vedeți un exemplu concret de astfel de eroare, verificați acest cod: |
57 | 57 |
|
58 | 58 | ```js run |
59 | 59 | [1, 2].forEach(alert) |
60 | 60 | ``` |
61 | 61 |
|
62 | | -No need to think about the meaning of the brackets `[]` and `forEach` yet. We'll study them later. For now, just remember the result of the code: it shows `1` then `2`. |
| 62 | +Nu este nevoie să ne gândim încă la semnificația parantezelor `[]` și `forEach`. O să le studiem mai târziu. Deocamdată, amintiți-vă doar rezultatul codului: este afișat `1` apoi` 2`. |
63 | 63 |
|
64 | | -Now, let's add an `alert` before the code and *not* finish it with a semicolon: |
| 64 | +Acum, să adăugăm un `alert` înainte de cod și să *nu* încheiem cu un punct și virgulă: |
65 | 65 |
|
66 | 66 | ```js run no-beautify |
67 | | -alert("There will be an error") |
| 67 | +alert("Va apărea o eroare") |
68 | 68 |
|
69 | 69 | [1, 2].forEach(alert) |
70 | 70 | ``` |
71 | 71 |
|
72 | | -Now if we run the code, only the first `alert` is shown and then we have an error! |
| 72 | +Acum dacă rulăm codul, doar primul `alert` este afișat iar apoi o să avem o eroare! |
73 | 73 |
|
74 | | -But everything is fine again if we add a semicolon after `alert`: |
| 74 | +Dar totul este bine din nou dacă adăugăm un punct și virgulă după `alert`: |
75 | 75 | ```js run |
76 | | -alert("All fine now"); |
| 76 | +alert("Totul este bine acum"); |
77 | 77 |
|
78 | 78 | [1, 2].forEach(alert) |
79 | 79 | ``` |
80 | 80 |
|
81 | | -Now we have the "All fine now" message followed by `1` and `2`. |
| 81 | +Acum o să avem mesajul "Totul este bine acum" urmat de `1` și `2`. |
82 | 82 |
|
83 | 83 |
|
84 | | -The error in the no-semicolon variant occurs because JavaScript does not assume a semicolon before square brackets `[...]`. |
| 84 | +Eroarea din varianta fără punct și virgulă apare deoarece JavaScript nu își asumă punct și virgulă înainte de paranteze pătrate `[...]`. |
85 | 85 |
|
86 | | -So, because the semicolon is not auto-inserted, the code in the first example is treated as a single statement. Here's how the engine sees it: |
| 86 | +Deci, pentru că punct și virgulă nu este inserat automat, codul din primul exemplu este tratat ca o singură instrucțiune. Iată cum îl vede motorul: |
87 | 87 |
|
88 | 88 | ```js run no-beautify |
89 | | -alert("There will be an error")[1, 2].forEach(alert) |
| 89 | +alert("Va apărea o eroare")[1, 2].forEach(alert) |
90 | 90 | ``` |
91 | 91 |
|
92 | | -But it should be two separate statements, not one. Such a merging in this case is just wrong, hence the error. This can happen in other situations. |
| 92 | +Dar ar trebui să fie două afirmații separate, nu una. O astfel de fuziune în acest caz este greșită, de unde și eroarea. Acest lucru se poate întâmpla în alte situații. |
93 | 93 | ```` |
94 | 94 |
|
95 | | -We recommend putting semicolons between statements even if they are separated by newlines. This rule is widely adopted by the community. Let's note once again -- *it is possible* to leave out semicolons most of the time. But it's safer -- especially for a beginner -- to use them. |
| 95 | +Vă recomandăm să puneți punct și virgulă între instrucțiuni, chiar dacă sunt separate prin linii noi. Această regulă este adoptată pe scară largă de comunitate. Să notăm încă o dată -- *este posibil* să omitem punctul și virgula de cele mai multe ori. Dar este mai sigur -- mai ales pentru un începător -- să o folosească. |
96 | 96 |
|
97 | | -## Comments |
| 97 | +## Comentarii |
98 | 98 |
|
99 | | -As time goes on, programs become more and more complex. It becomes necessary to add *comments* which describe what the code does and why. |
| 99 | +Pe măsură ce timpul trece, programele devin din ce în ce mai complexe. Devine necesar să adăugați *comentarii* care descriu ce face codul și de ce. |
100 | 100 |
|
101 | | -Comments can be put into any place of a script. They don't affect its execution because the engine simply ignores them. |
| 101 | +Comentariile pot fi puse în orice loc al unui script. Acestea nu afectează execuția acestuia, deoarece motorul pur și simplu le ignoră. |
102 | 102 |
|
103 | | -**One-line comments start with two forward slash characters `//`.** |
| 103 | +**Comentariile pe o singură linie încep cu două caractere de slash înainte `//`.** |
104 | 104 |
|
105 | | -The rest of the line is a comment. It may occupy a full line of its own or follow a statement. |
| 105 | +Restul liniei este un comentariu. Poate ocupa o linie completă proprie sau poate urma o declarație. |
106 | 106 |
|
107 | | -Like here: |
| 107 | +Ca aici: |
108 | 108 | ```js run |
109 | | -// This comment occupies a line of its own |
| 109 | +// Acest comentariu ocupă o linie proprie |
110 | 110 | alert('Hello'); |
111 | 111 |
|
112 | | -alert('World'); // This comment follows the statement |
| 112 | +alert('World'); // Acest comentariu urmează afirmația |
113 | 113 | ``` |
114 | 114 |
|
115 | | -**Multiline comments start with a forward slash and an asterisk <code>/*</code> and end with an asterisk and a forward slash <code>*/</code>.** |
| 115 | +**Comentariile pe mai multe linii încep cu un slash înainte și un asterisc <code>/*</code> și se încheie cu un asterisc și cu un slash înainte <code>*/</code>.** |
116 | 116 |
|
117 | | -Like this: |
| 117 | +În felul următor: |
118 | 118 |
|
119 | 119 | ```js run |
120 | | -/* An example with two messages. |
121 | | -This is a multiline comment. |
| 120 | +/* Un exemplu cu două mesaje. |
| 121 | +Acesta este un comentariu pe mai multe linii. |
122 | 122 | */ |
123 | 123 | alert('Hello'); |
124 | 124 | alert('World'); |
125 | 125 | ``` |
126 | 126 |
|
127 | | -The content of comments is ignored, so if we put code inside <code>/* ... */</code>, it won't execute. |
| 127 | +Conținutul comentariilor este ignorat, deci dacă punem cod în interior <code>/* ... */</code>, acesta nu o să se execute. |
128 | 128 |
|
129 | | -Sometimes it can be handy to temporarily disable a part of code: |
| 129 | +Uneori, poate fi util să dezactivați temporar o parte a codului: |
130 | 130 |
|
131 | 131 | ```js run |
132 | | -/* Commenting out the code |
| 132 | +/* Comentând codul |
133 | 133 | alert('Hello'); |
134 | 134 | */ |
135 | 135 | alert('World'); |
136 | 136 | ``` |
137 | 137 |
|
138 | | -```smart header="Use hotkeys!" |
139 | | -In most editors, a line of code can be commented out by pressing the `key:Ctrl+/` hotkey for a single-line comment and something like `key:Ctrl+Shift+/` -- for multiline comments (select a piece of code and press the hotkey). For Mac, try `key:Cmd` instead of `key:Ctrl`. |
| 138 | +```smart header="Folosiți tastele rapide!" |
| 139 | +În majoritatea editorilor, o linie de cod poate fi comentată apăsând tasta `key:Ctrl+/` pentru un comentariu pe o singură linie și `key:Ctrl+Shift+/` -- pentru comentarii pe mai multe linii (selectați o bucată de cod și apăsați tasta rapidă). Pentru Mac, încercați `key:Cmd` în loc de `key:Ctrl`. |
140 | 140 | ``` |
141 | 141 |
|
142 | | -````warn header="Nested comments are not supported!" |
143 | | -There may not be `/*...*/` inside another `/*...*/`. |
| 142 | +````warn header="Comentariile imbricate nu sunt suportate!" |
| 143 | +Nu poate exista `/*...*/` în interiorul altui `/*...*/`. |
144 | 144 |
|
145 | | -Such code will die with an error: |
| 145 | +Acest cod va genera o eroare: |
146 | 146 |
|
147 | 147 | ```js run no-beautify |
148 | 148 | /* |
149 | | - /* nested comment ?!? */ |
| 149 | + /* comentariu imbricat ?!? */ |
150 | 150 | */ |
151 | 151 | alert( 'World' ); |
152 | 152 | ``` |
153 | 153 | ```` |
154 | 154 |
|
155 | | -Please, don't hesitate to comment your code. |
| 155 | +Vă rugăm, nu ezitați să vă comentați codul. |
156 | 156 |
|
157 | | -Comments increase the overall code footprint, but that's not a problem at all. There are many tools which minify code before publishing to a production server. They remove comments, so they don't appear in the working scripts. Therefore, comments do not have negative effects on production at all. |
| 157 | +Comentariile cresc amprenta generală a codului, dar aceasta nu este deloc o problemă. Există multe instrumente care reduc codul înainte de publicarea pe un server de producție. Șterg comentariile, deci nu apar în scripturile de lucru. Prin urmare comentariile nu au deloc efecte negative asupra producției. |
158 | 158 |
|
159 | | -Later in the tutorial there will be a chapter <info:code-quality> that also explains how to write better comments. |
| 159 | +Mai târziu în tutorial va exista un capitol <info:code-quality> care explică, de asemenea, cum să scrieți comentarii mai bune. |
0 commit comments