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
Named Function Expression, or NFE, is a term for Function Expressions that have a name.
210
+
Named Function Expression, sau NFE, este un termen pentru Function Expressions care au un nume.
211
211
212
-
For instance, let's take an ordinary Function Expression:
212
+
De exemplu, să luăm o Function Expression obișnuită:
213
213
214
214
```js
215
215
letsayHi=function(who) {
216
216
alert(`Hello, ${who}`);
217
217
};
218
218
```
219
219
220
-
And add a name to it:
220
+
Și adăugați-i un nume:
221
221
222
222
```js
223
223
letsayHi=function*!*func*/!*(who) {
224
-
alert(`Hello, ${who}`);
224
+
alert(`Bună ziua, ${who}`);
225
225
};
226
226
```
227
227
228
-
Did we achieve anything here? What's the purpose of that additional`"func"` name?
228
+
Am realizat ceva aici? Care este scopul acelui nume suplimentar`"func"`?
229
229
230
-
First let's note, that we still have a Function Expression. Adding the name `"func"`after`function`did not make it a Function Declaration, because it is still created as a part of an assignment expression.
230
+
În primul rând, să notăm că avem încă o Function Expression. Adăugarea numelui `"func"`după`function`nu a făcut-o Function Declaration, deoarece aceasta este încă creată ca parte a unei expresii de atribuire.
231
231
232
-
Adding such a name also did not break anything.
232
+
De asemenea adăugarea unui astfel de nume nu a stricat nimic.
233
233
234
-
The function is still available as`sayHi()`:
234
+
Funcția este în continuare disponibilă ca`sayHi()`:
235
235
236
236
```js run
237
237
letsayHi=function*!*func*/!*(who) {
238
-
alert(`Hello, ${who}`);
238
+
alert(`Bună ziua, ${who}`);
239
239
};
240
240
241
-
sayHi("John"); //Hello, John
241
+
sayHi("John"); //Bună ziua, John
242
242
```
243
243
244
-
There are two special things about the name`func`, that are the reasons for it:
244
+
Există două lucruri speciale în legătură cu numele`func`, care sunt motivele pentru acesta:
245
245
246
-
1.It allows the function to reference itself internally.
247
-
2.It is not visible outside of the function.
246
+
1.Acesta permite funcției să facă referire la ea însăși în mod intern.
247
+
2.Nu este vizibil în afara funcției.
248
248
249
-
For instance, the function `sayHi`below calls itself again with `"Guest"`if no `who` is provided:
249
+
De exemplu, funcția `sayHi`de mai jos se apelează din nou pe sine cu `"Guest"`dacă nu este furnizat `who`:
250
250
251
251
```js run
252
252
letsayHi=function*!*func*/!*(who) {
253
253
if (who) {
254
-
alert(`Hello, ${who}`);
254
+
alert(`Bună ziua, ${who}`);
255
255
} else {
256
256
*!*
257
-
func("Guest"); //use func to re-call itself
257
+
func("Guest"); //folosește func pentru a se re-apela pe sine însăși
258
258
*/!*
259
259
}
260
260
};
261
261
262
-
sayHi(); //Hello, Guest
262
+
sayHi(); //Bună ziua, Guest
263
263
264
-
//But this won't work:
265
-
func(); // Error, func is not defined (not visible outside of the function)
264
+
//Dar acest lucru nu va funcționa:
265
+
func(); // Error, func is not defined (nu este vizibil în afara funcției)
266
266
```
267
267
268
-
Why do we use `func`? Maybe just use `sayHi`for the nested call?
268
+
De ce folosim `func`? Poate doar să folosim `sayHi`pentru apelul nested?
269
269
270
270
271
-
Actually, in most cases we can:
271
+
De fapt, în majoritatea cazurilor putem:
272
272
273
273
```js
274
274
letsayHi=function(who) {
@@ -282,12 +282,12 @@ let sayHi = function(who) {
282
282
};
283
283
```
284
284
285
-
The problem with that code is that `sayHi`may change in the outer code. If the function gets assigned to another variable instead, the code will start to give errors:
285
+
Problema cu acel cod este că `sayHi`se poate schimba în codul exterior. Dacă în schimb funcția este atribuită unei alte variabile, codul va începe să dea erori:
286
286
287
287
```js run
288
288
letsayHi=function(who) {
289
289
if (who) {
290
-
alert(`Hello, ${who}`);
290
+
alert(`Bună ziua, ${who}`);
291
291
} else {
292
292
*!*
293
293
sayHi("Guest"); // Error: sayHi is not a function
@@ -298,40 +298,40 @@ let sayHi = function(who) {
298
298
let welcome = sayHi;
299
299
sayHi =null;
300
300
301
-
welcome(); //Error, the nested sayHi call doesn't work any more!
301
+
welcome(); //Eroare, apelul nested sayHi nu mai funcționează!
302
302
```
303
303
304
-
That happens because the function takes`sayHi`from its outer lexical environment. There's no local`sayHi`, so the outer variable is used. And at the moment of the call that outer `sayHi`is`null`.
304
+
Asta se întâmplă deoarece funcția preia`sayHi`din mediul său lexical extern. Nu există un`sayHi` local, așa că se folosește variabila exterioară. Iar în momentul apelului, acea `sayHi`exterioară este`null`.
305
305
306
-
The optional name which we can put into the Function Expression is meant to solve exactly these kinds of problems.
306
+
Numele opțional pe care îl putem pune în Function Expression este menit să rezolve exact acest tip de probleme.
Now it works, because the name `"func"`is function-local. It is not taken from outside (and not visible there). The specification guarantees that it will always reference the current function.
327
+
Acum funcționează, deoarece numele `"func"`este local pentru funcții. Nu este preluat din exterior (și nu este vizibil acolo). Specificația garantează că va face întotdeauna referire la funcția curentă.
328
328
329
-
The outer code still has its variable `sayHi`or`welcome`. And`func`is an "internal function name", the way for the function to can call itself reliably.
329
+
Codul exterior are în continuare variabila `sayHi`sau`welcome`. Iar`func`este un "nume de funcție intern", modul în care funcția se poate apela singură în mod fiabil.
330
330
331
-
```smart header="There's no such thing for Function Declaration"
332
-
The "internal name" feature described here is only available for Function Expressions, not for Function Declarations. For Function Declarations, there is no syntax for adding an "internal" name.
331
+
```smart header="Nu există așa ceva pentru Function Declaration"
332
+
Caracteristica "nume intern" descrisă aici este disponibilă doar pentru Function Expressions, nu și pentru Function Declarations. Pentru Function Declarations, nu există o sintaxă pentru adăugarea unui nume "intern".
333
333
334
-
Sometimes, when we need a reliable internal name, it's the reason to rewrite a Function Declaration to Named Function Expression form.
334
+
Uneori, atunci când avem nevoie de un nume intern fiabil, acesta este motivul pentru a rescrie o Function Declaration sub forma unei Named Function Expression.
0 commit comments