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
leviding
merged 5 commits into
javascript-tutorial:zh-hans
from
NoName4Me:translate-01-06-06
May 13, 2018
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
1-js/06-advanced-functions/06-function-object/2-counter-inc-dec/solution.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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`。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
|
@@ -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: | ||
在我们的场景里,只是返回了函数,没有调用它: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 『在我们的场景里,只是返回了函数,没有调用它:』=>『在我们的例子中,只是返回了函数,并没有调用它:』 |
||
|
||
```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` 来实现转换。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『得』=>『的』