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

Generator 的语法一章的wrapper包装例子是不是可以简化 #1156

Closed
bsc2xp opened this issue Jun 9, 2023 · 1 comment
Closed

Comments

@bsc2xp
Copy link

bsc2xp commented Jun 9, 2023

在 Generator 的语法那章里, 为了解释 next 方法传参的机制,给了一个例子,如下:
`
function wrapper(generatorFunction) {
return function (...args) {
let generatorObject = generatorFunction(...args);
generatorObject.next();
return generatorObject;
};
}

const wrapped = wrapper(function* () {
console.log(First input: ${yield});
return 'DONE';
});

wrapped().next('hello!')
`

我一直没想明白 wrapper 为什么还要返回一个 function ,就这个示例的目的而言写成下面这样不行吗:

`function wrapper(generatorFunction) {
let generatorObject = generatorFunction();
generatorObject.next();
return generatorObject;
}

const wrapped = wrapper(function* () {
console.log(First input: ${yield});
return 'DONE';
});

wrapped.next('hello!')
`

@ruanyf
Copy link
Owner

ruanyf commented Jun 15, 2023

这样简单一点,原文是构造一个 Generator API 兼容对象。

@ruanyf ruanyf closed this as completed Jun 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants