Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1-js/06-advanced-functions/06-function-object/ #83

Merged
merged 5 commits into from May 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,2 +1,2 @@

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`.
该解在局部变量中使用 `count`,但是在 `counter` 中直接添加了方法。它们共享同一个外部词法环境,并且可以访问当前 `count`
Expand Up @@ -2,14 +2,14 @@ importance: 5

---

# Set and decrease for counter
# 为 `counter` 添加 `set` 和 `decrease` 方法

Modify the code of `makeCounter()` so that the counter can also decrease and set the number:
修改 `makeCounter()` 代码,使得 counter 可以减一和赋值:

- `counter()` should return the next number (as before).
- `counter.set(value)` should set the `count` to `value`.
- `counter.decrease(value)` should decrease the `count` by 1.
- `counter()` 应该返回下一个数字(同以前逻辑)。
- `counter.set(value)` 应该设置 `count` `value`
- `counter.decrease(value)` 应该把 `count` 减 1。

See the sandbox code for the complete usage example.
查看沙箱代码获取完整使用示例。

P.S. You can use either a closure or the function property to keep the current count. Or write both variants.
P.S. 你也可以使用闭包或者函数属性来保持当前的计数,或者两者的变体。
@@ -1,9 +1,9 @@

1. For the whole thing to work *anyhow*, the result of `sum` must be function.
2. That function must keep in memory the current value between calls.
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.
1. **无论**整体如何工作,`sum` 的结果必须是函数。
2. 这个函数必须在内存里保留调用之间的当前值。
3. 根据任务,当函数被用在 `==` 左右时,它必须返回数字。函数是对象,所以转换如 <info:object-toprimitive> 章节所述,我们可以提供自己的方法来返回数字。

Now the code:
代码如下:

```js run
function sum(a) {
Expand All @@ -28,28 +28,28 @@ alert( sum(6)(-1)(-2)(-3) ); // 0
alert( sum(0)(1)(2)(3)(4)(5) ); // 15
```

Please note that the `sum` function actually works only once. It returns function `f`.
请注意 `sum` 函数只工作一次,它返回了函数 `f`

Then, on each subsequent call, `f` adds its parameter to the sum `currentSum`, and returns itself.
然后,接下来的每一次调用,`f` 都会把自己的参数加到求和 `currentSum` 上,然后返回自己。

**There is no recursion in the last line of `f`.**
**`f` 的最后一行没有递归。**

Here is what recursion looks like:
递归是这样子的:

```js
function f(b) {
currentSum += b;
return f(); // <-- recursive call
return f(); // <-- 递归调用
}
```

And in our case, we just return the function, without calling it:
在我们的例子里,只是返回了函数,并没有调用它:

```js
function f(b) {
currentSum += b;
return f; // <-- does not call itself, returns itself
return f; // <-- 没有调用自己,只是返回了自己
}
```

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.
这个 `f` 会被用于下一次调用,然后再次返回自己,按照需要重复。然后,当它被用做数字或字符串时 —— `toString` 返回 `currentSum`。我们也可以使用 `Symbol.toPrimitive` 或者 `valueOf` 来实现转换。
Expand Up @@ -2,9 +2,9 @@ importance: 2

---

# Sum with an arbitrary amount of brackets
# 任意多个括号求和

Write function `sum` that would work like this:
写一个函数 `sum`,它有这样的功能:

```js
sum(1)(2) == 3; // 1 + 2
Expand All @@ -14,4 +14,4 @@ sum(6)(-1)(-2)(-3) == 0
sum(0)(1)(2)(3)(4)(5) == 15
```

P.S. Hint: you may need to setup custom object to primitive conversion for your function.
P.S. 提示:你可能需要创建自定义对象来为你的函数提供基本类型转换。