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
Many JavaScript built-in functions support an arbitrary number of arguments.
3
+
تتوقّع العديد من دوال جافاسكربت المضمّنة في اللغة عددًا من الوُسطاء لا ينتهي.
4
4
5
-
For instance:
5
+
مثال:
6
6
7
-
-`Math.max(arg1, arg2, ..., argN)` -- returns the greatest of the arguments.
8
-
-`Object.assign(dest, src1, ..., srcN)` -- copies properties from `src1..N` into `dest`.
9
-
- ...and so on.
7
+
-`Math.max(arg1, arg2, ..., argN)` -- يُعيد أكبر وسيط من الوُسطاء.
8
+
-`Object.assign(dest, src1, ..., srcN)` -- ينسخ الخصائص من `src1..N` إلى `dest`.
9
+
- ...وهكذا.
10
10
11
-
In this chapter we'll learn how to do the same. And also, how to pass arrays to such functions as parameters.
12
11
13
-
## Rest parameters `...`
12
+
سنتعلّم في هذا الفصل كيف نفعل ذلك أيضًا. كما وكيف نمرّر المصفوفات إلى هذه الدوال على أنّها مُعاملات.
13
+
14
+
## المُعاملات «البقية» `...`
15
+
16
+
يمكن أن ننادي الدالة بأيّ عدد من الوُسطاء كيفما كانت معرّفة الدالة.
17
+
18
+
هكذا:
14
19
15
-
A function can be called with any number of arguments, no matter how it is defined.
16
20
17
-
Like here:
18
21
```js run
19
22
functionsum(a, b) {
20
23
return a + b;
@@ -23,14 +26,14 @@ function sum(a, b) {
23
26
alert( sum(1, 2, 3, 4, 5) );
24
27
```
25
28
26
-
There will be no error because of "excessive" arguments. But of course in the result only the first two will be counted.
29
+
لن ترى أيّ خطأ بسبب تلك الوُسطاء «الزائدة». ولكن طبعًا فالنتيجة لن تأخذ بالحسبان إلا أوّل اثنين.
27
30
28
-
The rest of the parameters can be included in the function definition by using three dots `...` followed by the name of the array that will contain them. The dots literally mean "gather the remaining parameters into an array".
31
+
يمكن تضمين بقية المُعاملات في تعريف الدالة باستعمال الثلاث نقاط `...` ثمّ اسم المصفوفة التي ستحتويهم. تعني تلك النقط حرفيًا «اجمع المُعاملات الباقية في مصفوفة».
29
32
30
-
For instance, to gather all arguments into array `args`:
33
+
فمثلًا لجمع كلّ الوُسطاء في المصفوفة `args`:
31
34
32
35
```js run
33
-
functionsumAll(...args) { //args is the name for the array
36
+
functionsumAll(...args) { //اسم المصفوفة هو args
34
37
let sum =0;
35
38
36
39
for (let arg of args) sum += arg;
@@ -43,71 +46,74 @@ alert( sumAll(1, 2) ); // 3
43
46
alert( sumAll(1, 2, 3) ); // 6
44
47
```
45
48
46
-
We can choose to get the first parameters as variables, and gather only the rest.
49
+
يمكن لو أردنا أن نأخذ المُعاملات الأولى في متغيّرات ونجمع البقية فقط.
47
50
48
-
Here the first two arguments go into variables and the rest go into `titles` array:
51
+
هنا نأخذ الوسيطين الأوليين في متغيرات والباقي نرميه في المصفوفة `titles`:
````warn header="The rest parameters must be at the end"
65
-
The rest parameters gather all remaining arguments, so the following does not make sense and causes an error:
68
+
````warn header="يجب أن تُترك المُعاملات البقية إلى النهاية"
69
+
تجمع المُعاملات البقية كلّ الوُسطاء التي بقيت. وبهذا فالآتي ليس منطقيًا وسيتسبّب بخطأ:
66
70
67
71
```js
68
-
function f(arg1, ...rest, arg2) { // arg2 after ...rest ?!
69
-
// error
72
+
function f(arg1, ...rest, arg2) { // الوسيط arg2 بعد ...البقية؟!
73
+
// خطأ
70
74
}
75
+
71
76
```
72
77
73
-
The `...rest` must always be last.
78
+
يجب أن يكون `...rest` الأخير دومًا.
74
79
````
75
80
76
-
## The "arguments" variable
81
+
## متغيّر الوُسطاء arguments
82
+
83
+
هناك كائن آخر شبيه بالمصفوفات يُدعى `arguments` ويحتوي على كلّ الوُسطاء حسب ترتيب فهارسها.
77
84
78
-
There is also a special array-like object named `arguments` that contains all arguments by their index.
85
+
مثال:
79
86
80
-
For instance:
81
87
82
88
```js run
83
89
functionshowName() {
84
90
alert( arguments.length );
85
91
alert( arguments[0] );
86
92
alert( arguments[1] );
87
93
88
-
//it's iterable
94
+
//المصفوفة مُتعدَّدة
89
95
// for(let arg of arguments) alert(arg);
90
96
}
91
97
92
-
//shows: 2, Julius, Caesar
98
+
//تعرض: 2, Julius, Caesar
93
99
showName("Julius", "Caesar");
94
100
95
-
//shows: 1, Ilya, undefined (no second argument)
101
+
//تعرض: 1, Ilya, undefined (ما من مُعطى ثانٍ)
96
102
showName("Ilya");
103
+
97
104
```
98
105
99
-
In old times, rest parameters did not exist in the language, and using `arguments` was the only way to get all arguments of the function. And it still works, we can find it in the old code.
106
+
قديمًا لم تكن المُعاملات البقية موجودة في اللغة ولم يكن لدينا سوى استعمال `arguments` لنجلب كلّ مُعاملات الدالة. وما زالت تعمل الطريقة إلى يومنا هذا ويمكن أن تراها في الشيفرات القديمة.
100
107
101
-
But the downside is that although `arguments` is both array-like and iterable, it's not an array. It does not support array methods, so we can't call `arguments.map(...)` for example.
108
+
ولكن السلبية هنا هي أنّ `arguments` ليست مصفوفة (على الرغم من أنّها شبيهة بالمصفوفات ومُتعدّدة). بهذا لا تدعم توابِع المصفوفات فلا ينفع أن نستدعي عليها `arguments.map(...)` مثلًا.
102
109
103
-
Also, it always contains all arguments. We can't capture them partially, like we did with rest parameters.
110
+
كما وأنّها تحتوي على كل الوُسطاء دومًا. لا يمكن أن نأخذ منها ما نريد كما نفعل مع المُعاملات البقية.
104
111
105
-
So when we need these features, then rest parameters are preferred.
112
+
لهذا متى ما احتجنا إلى ميزة كهذه، فالأفضل استعمال المُعاملات البقية بدلًا من `arguments`.
106
113
107
-
````smart header="Arrow functions do not have `\"arguments\"`"
108
-
If we access the `arguments` object from an arrow function, it takes them from the outer "normal" function.
114
+
**ليس للدوال السهمية `"arguments"`**
115
+
لو حاولت الوصول إلى كائن الوُسطاء `arguments` من داخل الدالة السهمية، فستستلم الناتج من الدالة «الطبيعية» الخارجية. إليك مثالًا:
109
116
110
-
Here's an example:
111
117
112
118
```js run
113
119
functionf() {
@@ -117,173 +123,182 @@ function f() {
117
123
118
124
f(1); // 1
119
125
```
126
+
كما نذكر فليس للدوال السهمية قيمة `this` تخصّها، أمّا الآن صرنا نعلم بأنّ ليس لها كائن `arguments` أيضًا.
120
127
121
-
As we remember, arrow functions don't have their own `this`. Now we know they don't have the special `arguments` object either.
122
-
````
123
128
129
+
## مُعامل التوزيع [#spread-syntax]
124
130
125
-
## Spread syntax [#spread-syntax]
131
+
رأينا كيف نأخذ مصفوفة من قائمة من المُعطيات.
126
132
127
-
We've just seen how to get an array from the list of parameters.
133
+
ولكن ماذا لو أردنا العكس من ذلك؟
128
134
129
-
But sometimes we need to do exactly the reverse.
135
+
فمثلًا لنقل أردنا استعمال الدالة المبنية في اللغة Math.max والتي تُعيد أكبر عدد من القائمة:
130
136
131
-
For instance, there's a built-in function [Math.max](mdn:js/Math/max) that returns the greatest number from a list:
132
137
133
138
```js run
134
139
alert( Math.max(3, 5, 1) ); // 5
135
140
```
136
141
137
-
Now let's say we have an array `[3, 5, 1]`. How do we call `Math.max` with it?
138
142
139
-
Passing it "as is" won't work, because `Math.max` expects a list of numeric arguments, not a single array:
143
+
لنقل أنّ لدينا المصفوفة `[3, 5, 1]`. كيف نستدعي `Math.max` عليها؟
144
+
145
+
لا ينفع تمريرها «كما هي» لأنّ `Math.max` يتوقّع قائمةً بالوُسطاء العددية لا مصفوفة واحدة:
146
+
140
147
141
148
```js run
142
149
let arr = [3, 5, 1];
143
150
144
-
*!*
145
151
alert( Math.max(arr) ); // NaN
146
-
*/!*
152
+
147
153
```
148
154
149
-
And surely we can't manually list items in the code `Math.max(arr[0], arr[1], arr[2])`, because we may be unsure how many there are. As our script executes, there could be a lot, or there could be none. And that would get ugly.
155
+
وطبعًا لا يمكن أن نفكّ عناصر القائمة يدويًا في الشيفرة `Math.max(arr[0], arr[1], arr[2])` لأنّنا في حالات لا نعرف كم من عنصر هناك أصلًا. وما إن يتنفّذ السكربت يمكن أن يكون فيه أكبر مما كتبناه أو حتّى لا شيء أصلًا، وسنحصد لاحقًا ما جنته هذه الشيفرة.
150
156
151
-
*Spread syntax* to the rescue! It looks similar to rest parameters, also using `...`, but does quite the opposite.
157
+
عاش مُنقذنا *مُعامل التوزيع*! عاش عاش عاش! من بعيد نراه مشابهًا تمامًا للمُعاملات البقية، كما ويستعمل `...`، إلّا أنّ وظيفته هي العكس تمامًا.
152
158
153
-
When `...arr` is used in the function call, it "expands" an iterable object `arr` into the list of arguments.
159
+
فحين نستعمل `...arr` في استدعاء الدالة، «يتوسّع» الكائن المُتعدَّد `...arr` إلى قائمة من الوُسطاء.
160
+
161
+
فمثلًا نعود إلى `Math.max`:
154
162
155
-
For `Math.max`:
156
163
157
164
```js run
158
165
let arr = [3, 5, 1];
159
166
160
-
alert( Math.max(...arr) ); // 5 (spread turns array into a list of arguments)
167
+
// 5 (يحوّل التوزيع المصفوفة إلى قائمة من الوُسطاء)
168
+
alert( Math.max(...arr) );
169
+
161
170
```
162
171
163
-
We also can pass multiple iterables this way:
172
+
يمكن أيضًا أن نمرّر أكثر من مُتعدَّد واحد بهذه الطريقة:
164
173
165
174
```js run
166
175
let arr1 = [1, -2, 3, 4];
167
176
let arr2 = [8, 3, -8, 1];
168
177
169
178
alert( Math.max(...arr1, ...arr2) ); // 8
179
+
170
180
```
171
181
172
-
We can even combine the spread syntax with normal values:
استعملنا في الأمثلة أعلاه مصفوفة لنشرح مُعامل التوزيع، إلّا أنّ المُتعدَّدات أيًا كانت تنفع أيضًا.
194
205
195
-
In the examples above we used an array to demonstrate the spread syntax, but any iterable will do.
206
+
فمثلًا نستعمل هنا مُعامل التوزيع لنحوّل السلسلة النصية إلى مصفوفة محارف:
196
207
197
-
For instance, here we use the spread syntax to turn the string into array of characters:
198
208
199
209
```js run
200
210
let str ="Hello";
201
211
202
212
alert( [...str] ); // H,e,l,l,o
213
+
203
214
```
204
215
205
-
The spread syntax internally uses iterators to gather elements, the same way as `for..of` does.
216
+
يستعمل مُعامل التوزيع هذا داخليًا المُعدِّدات لجمع العناصر، كما تفعل حلقة `for..of`.
217
+
218
+
لذا لو استلمت `for..of` سلسلةً نصيّة فتُعيد لنا المحارف وتصير `...str` بالقيمة `"H","e","l","l","o"`. وهكذا تُمرّر قائمة المحارف إلى مُهيّئ المصفوفة `[...str]`.
206
219
207
-
So, for a string, `for..of` returns characters and `...str` becomes `"H","e","l","l","o"`. The list of characters is passed to array initializer `[...str]`.
220
+
يمكننا أيضًا لهذه المهمة استعمال `Array.from` إذ أنّه يحوّل المُتعدَّد (مثل السلاسل النصية) إلى مصفوفة:
208
221
209
-
For this particular task we could also use `Array.from`, because it converts an iterable (like a string) into an array:
210
222
211
223
```js run
212
224
let str ="Hello";
213
225
214
-
// Array.from converts an iterable into an array
226
+
//يُحوّل Array.from المُتعدَّد إلى مصفوفة
215
227
alert( Array.from(str) ); // H,e,l,l,o
216
228
```
217
229
218
-
The result is the same as `[...str]`.
230
+
ناتجه هو ذات ناتج `[...str]`.
219
231
220
-
But there's a subtle difference between `Array.from(obj)` and `[...obj]`:
232
+
ولكن... هناك فرق ضئيل بين `Array.from(obj)` و`[...obj]`:
221
233
222
-
- `Array.from` operates on both array-likes and iterables.
223
-
- The spread syntax works only with iterables.
234
+
-يعمل `Array.from` على الشبيهات بالمصفوفات والمُتعدَّدات.
235
+
-ويعمل مُعامل التوزيع على المُتعدَّدات فقط لا غير.
224
236
225
-
So, for the task of turning something into an array, `Array.from` tends to be more universal.
237
+
لذا لو أردت تحويل شيء إلى مصفوفة فالتابِع `Array.from` أكثر استعمالًا وشيوعًا.
226
238
239
+
## الحصول علي نسخة من المصفوفة/الكائن
227
240
228
-
## Get a new copy of an array/object
241
+
تتذكر عندما تحدثنا عن `Object.assign()`[في الماضي](https://javascript.info/object#cloning-and-merging-object-assign)?
229
242
230
-
Remember when we talked about `Object.assign()` [in the past](https://javascript.info/object#cloning-and-merging-object-assign)?
243
+
يمكن أن تقوم بالمثل عن طريق `...`.
231
244
232
-
It is possible to do the same thing with the spread syntax.
233
245
234
246
```js run
235
247
let arr = [1, 2, 3];
236
-
let arrCopy = [...arr]; // spread the array into a list of parameters
237
-
// then put the result into a new array
248
+
let arrCopy = [...arr]; // ننشر المصفوفة إلي قائمة من المعاملات
249
+
// ثم نضع الناتج في مصفوفة جديدة
250
+
251
+
// هل المصفوفات تمتلك نفس القيمة؟
252
+
alert(JSON.stringify(arr) ===JSON.stringify(arrCopy)); // صحيح
This way of copying an object is much shorter than `let objCopy = Object.assign({}, obj);` or for an array `let arrCopy = Object.assign([], arr);` so we prefer to use it whenever we can.
283
+
هذه الطريقة أقصر بكثير من `let objCopy = Object.assign({}, obj);`
0 commit comments