-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix:示例代码错误 #1239
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
Fix:示例代码错误 #1239
Conversation
javascript 课程的【函数进阶内容】章节,【装饰器模式和转发,call/apply】小节中呼叫转移的最简形式示例代码,其中的函数缺少入参,会使其他人产生疑问
|
@@ -319,7 +319,7 @@ func.apply(context, args); | |||
这是它的最简形式: | |||
|
|||
```js | |||
let wrapper = function() { | |||
let wrapper = function(func) { |
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.
这里如果加入参的话需要增加一个闭包
原文里意图是把 wrapper 当作 func 一样使用,所以 apply 这里有个 arguments 的透传。即 fn(xxx) 等同于 wrapper(xxxx),这么改 arguments 读了 func 又作为参数传给了自己就不对了
传 func 的话就应该是
let fn = function(func) {
function wrapper() {
return func.apply(this, arguments);
}
return wrapper;
};
之后通过 fn(x) 得到 x 的 wrapper
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.
这个例子主要还是想用 wrapper 的 apply 来说明执行和 func 一样,所以 wrapper 的 arguments 直接透传给了 fn
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.
这个例子主要还是想用 wrapper 的 apply 来说明执行和 func 一样,所以 wrapper 的 arguments 直接透传给了 fn
确实是这样,我理解错了,这里本意是一个用于返回的包装器
Please make the requested changes. After it, add a comment "/done". |
3 similar comments
Please make the requested changes. After it, add a comment "/done". |
Please make the requested changes. After it, add a comment "/done". |
Please make the requested changes. After it, add a comment "/done". |
目标章节:1-js/06-advanced-functions/09-call-apply-decorators
呼叫转移的最简形式示例代码,其中的函数缺少入参,会使其他人产生疑问
在本文的总结部分也有类似的问题,考虑到使用的是
original
,它可以指代原始对象函数,看上去可以不做修改当前上游最新 commit:此处填写本项目英文版 https://github.com/javascript-tutorial/en.javascript.info 的最新 commit,例如 b03ca00
本 PR 所做更改如下: