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
În practică, însă, majoritatea funcțiilor au un nume.
79
79
80
-
## The "length" property
80
+
## Proprietatea "length"
81
81
82
-
There is another built-in property "length" that returns the number of function parameters, for instance:
82
+
Există o altă proprietate încorporată "length" care returnează numărul de parametri ai funcției, de exemplu:
83
83
84
84
```js run
85
85
functionf1(a) {}
@@ -91,20 +91,20 @@ alert(f2.length); // 2
91
91
alert(many.length); // 2
92
92
```
93
93
94
-
Here we can see that rest parameters are not counted.
94
+
Aici putem vedea că parametrii rest nu sunt numărați.
95
95
96
-
The`length`property is sometimes used for [introspection](https://en.wikipedia.org/wiki/Type_introspection)in functions that operate on other functions.
96
+
Proprietatea`length`este uneori utilizată pentru [introspecție](https://en.wikipedia.org/wiki/Type_introspection)în funcțiile care operează pe alte funcții.
97
97
98
-
For instance, in the code below the `ask`function accepts a `question`to ask and an arbitrary number of `handler`functions to call.
98
+
De exemplu, în codul de mai jos funcția `ask`acceptă o `question`pentru a întreba și un număr arbitrar de funcții `handler`pentru a fi apelate.
99
99
100
-
Once a user provides their answer, the function calls the handlers. We can pass two kinds of handlers:
100
+
Odată ce un utilizator le oferă răspunsul, funcția apelează handlerii. Putem trece două tipuri de handlers:
101
101
102
-
-A zero-argument function, which is only called when the user gives a positive answer.
103
-
-A function with arguments, which is called in either case and returns an answer.
102
+
-O funcție cu zero argumente, care este apelată doar atunci când utilizatorul dă un răspuns pozitiv.
103
+
-O funcție cu argumente, care este apelată în oricare caz și care returnează un răspuns.
104
104
105
-
To call `handler`the right way, we examine the `handler.length` property.
105
+
Pentru a apela `handler`în mod corect, examinăm proprietatea `handler.length`.
106
106
107
-
The idea is that we have a simple, no-arguments handler syntax for positive cases (most frequent variant), but are able to support universal handlers as well:
107
+
Ideea este că avem o sintaxă handler simplă, fără argumente pentru cazurile pozitive (varianta cea mai frecventă), dar suntem capabili să suportăm și handlere universale:
108
108
109
109
```js run
110
110
functionask(question, ...handlers) {
@@ -120,47 +120,47 @@ function ask(question, ...handlers) {
120
120
121
121
}
122
122
123
-
//for positive answer, both handlers are called
124
-
//for negative answer, only the second one
125
-
ask("Question?", () =>alert('You said yes'), result=>alert(result));
123
+
//pentru răspuns pozitiv, ambii gestionari sunt apelați
This is a particular case of so-called [polymorphism](https://en.wikipedia.org/wiki/Polymorphism_(computer_science)) -- treating arguments differently depending on their type or, in our case depending on the`length`. The idea does have a use in JavaScript libraries.
128
+
Acesta este un caz particular al așa-numitului [polimorfism](https://en.wikipedia.org/wiki/Polymorphism_(computer_science)) -- tratarea argumentelor diferit în funcție de tipul lor sau, în cazul nostru în funcție de`length`. Ideea are o utilizare în bibliotecile JavaScript.
129
129
130
-
## Custom properties
130
+
## Proprietăți custom
131
131
132
-
We can also add properties of our own.
132
+
De asemenea putem adăuga proprietăți proprii.
133
133
134
-
Here we add the `counter`property to track the total calls count:
134
+
Aici adăugăm proprietatea `counter`pentru a urmări numărul total de apeluri:
135
135
136
136
```js run
137
137
functionsayHi() {
138
-
alert("Hi");
138
+
alert("Salut");
139
139
140
140
*!*
141
-
//let's count how many times we run
141
+
//să numărăm de câte ori se execută
142
142
sayHi.counter++;
143
143
*/!*
144
144
}
145
-
sayHi.counter=0; //initial value
145
+
sayHi.counter=0; //valoare inițială
146
146
147
-
sayHi(); //Hi
148
-
sayHi(); //Hi
147
+
sayHi(); //Salut
148
+
sayHi(); //Salut
149
149
150
-
alert( `Called ${sayHi.counter}times` ); //Called 2 times
150
+
alert( `Apelat de ${sayHi.counter}ori` ); //Apelat de 2 ori
151
151
```
152
152
153
-
```warn header="A property is not a variable"
154
-
A property assigned to a function like `sayHi.counter = 0` does *not* define a local variable `counter` inside it. In other words, a property `counter` and a variable `let counter` are two unrelated things.
153
+
```warn header="O proprietate nu este o variabilă"
154
+
O proprietate atribuită unei funcții precum `sayHi.counter = 0` *nu* definește o variabilă locală `counter` în interiorul ei. Cu alte cuvinte, o proprietate `counter` și o variabilă `let counter` sunt două lucruri fără legătură.
155
155
156
-
We can treat a function as an object, store properties in it, but that has no effect on its execution. Variables are not function properties and vice versa. These are just parallel worlds.
156
+
Putem trata o funcție ca pe un obiect, putem stoca proprietăți în ea, dar acest lucru nu are niciun efect asupra execuției sale. Variabilele nu sunt proprietăți ale funcțiilor și viceversa. Acestea sunt doar lumi paralele.
157
157
```
158
158
159
-
Function properties can replace closures sometimes. For instance, we can rewrite the counter function example from the chapter <info:closure>to use a function property:
159
+
Proprietățile funcțiilor pot înlocui closures uneori. De exemplu, putem rescrie exemplul funcției counter din capitolul <info:closure>pentru a folosi o proprietate de funcție:
160
160
161
161
```js run
162
162
functionmakeCounter() {
163
-
//instead of:
163
+
//în loc de:
164
164
// let count = 0
165
165
166
166
functioncounter() {
@@ -177,11 +177,11 @@ alert( counter() ); // 0
177
177
alert( counter() ); // 1
178
178
```
179
179
180
-
The`count`is now stored in the function directly, not in its outer Lexical Environment.
180
+
Acum`count`este stocat direct în funcție, nu în mediul său Lexical Environment.
181
181
182
-
Is it better or worse than using a closure?
182
+
Este mai bine sau mai rău decât utilizarea unui closure?
183
183
184
-
The main difference is that if the value of `count`lives in an outer variable, then external code is unable to access it. Only nested functions may modify it. And if it's bound to a function, then such a thing is possible:
184
+
Principala diferență este că dacă valoarea lui `count`trăiește într-o variabilă exterioară, atunci codul extern nu o poate accesa. Doar funcțiile nested o pot modifica. Iar dacă este legată de o funcție, atunci un astfel de lucru este posibil:
185
185
186
186
```js run
187
187
functionmakeCounter() {
@@ -203,7 +203,7 @@ alert( counter() ); // 10
203
203
*/!*
204
204
```
205
205
206
-
So the choice of implementation depends on our aims.
206
+
Așadar alegerea implementării depinde de obiectivele noastre.
0 commit comments