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
The solution uses `count` in the local variable, but addition methods are written right into the `counter`. They share the same outer lexical environment and also can access the current `count`.
2
+
الحل يستخدم `count` في المتغير المحلي, لكن الوظائف الإضافية تمت كتابتها بجانب `counter`.
3
+
إنهم يتشاركون نفس الحسد اللغوي ويمكنهم أيضاً الوصول إلى القيمة الحالية `count`.
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/solution.md
+18-14Lines changed: 18 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,10 @@
1
+
1.*أيّما كانت* الطريقة التي سنستعملها ليعمل هذا الشيء، فلا بدّ أن تُرجع `sum` دالة.
2
+
2. على تلك الدالة أن تحفظ القيمة الحالية بين كلّ استدعاء والآخر داخل الذاكرة.
3
+
3. حسب المهمّة المُعطاة، يجب أن تتحول الدالة إلى عدد حين نستعملها في `==`. الدوال كائنات لذا فعملية التحويل ستنفع كما شرحنا في فصل «التحويل من كائن إلى قيمة أولية»، ويمكن أن نقدّم تابِعًا خاصًا يُعيد ذلك العدد.
1
4
2
-
1. For the whole thing to work *anyhow*, the result of `sum` must be function.
3
-
2. That function must keep in memory the current value between calls.
4
-
3. According to the task, the function must become the number when used in `==`. Functions are objects, so the conversion happens as described in the chapter <info:object-toprimitive>, and we can provide our own method that returns the number.
Please note that the `sum` function actually works only once. It returns function `f`.
30
+
لاحظ بأنّ دالة `sum` تعمل مرّة واحدة فقط لا غير، وتُعيد الدالة `f`.
32
31
33
-
Then, on each subsequent call, `f` adds its parameter to the sum `currentSum`, and returns itself.
32
+
وبعدها في كلّ استدعاء يليها، تُضيف `f` المُعامل إلى المجموع `currentSum` وتُعيد نفسها.
34
33
35
-
**There is no recursion in the last line of `f`.**
34
+
**لا نستعمل التعاود في آخر سطر من `f`.**
36
35
37
-
Here is what recursion looks like:
36
+
هذا شكل التعاود:
38
37
39
38
```js
40
39
functionf(b) {
41
40
currentSum += b;
42
-
returnf(); // <-- recursive call
41
+
returnf(); // <-- استدعاء تعاودي
43
42
}
44
43
```
45
44
46
-
And in our case, we just return the function, without calling it:
45
+
بينما في حالتنا نُعيد الدالة دون استدعائها:
47
46
48
47
```js
49
48
functionf(b) {
50
49
currentSum += b;
51
-
return f; // <-- does not call itself, returns itself
50
+
return f; // <-- لا تستدعي نفسها، بل تُعيد نفسها
52
51
}
53
52
```
54
53
55
-
This `f` will be used in the next call, again return itself, so many times as needed. Then, when used as a number or a string -- the `toString` returns the `currentSum`. We could also use `Symbol.toPrimitive` or `valueOf` here for the conversion.
54
+
وستُستعمل `f` هذه في الاستدعاء التالي، وتُعيد نفسها ثانيةً مهما لزم. وبعدها حين نستعمل العدد أو السلسلة النصية، يُعيد التابِع `toString` المجموع `currentSum`. يمكن أيضًا أن نستعمل `Symbol.toPrimitive` أو `valueOf` لإجراء عملية التحويل.
55
+
56
+
ترجمة -وبتصرف- للفصل [Function object, NFE](https://javascript.info/function-object) من كتاب [The JavaScript language](https://javascript.info/js)
0 commit comments