javascript - yield如何作为javascript中的参数工作
问题描述
我在学习 yield 并想知道 yield 作为函数的参数时遇到了这段代码。在这个地方看起来像一个荣耀的回归
export function * throttle(func, time) {
let timerID = null;
function throttled(arg) {
clearTimeout(timerID);
timerID = setTimeout(func.bind(window, arg), time); // what does this do????
}
while(true) throttled(yield);
}
export class GeneratorThrottle {
constuctor() {};
start = () => {
thr = throttle(console.log, 3000);
thr.next('');
};
toString = () => {
console.log(throttle);
console.log('start =', this.start);
};
};
解决方案
使用next
方法,您可以将数据作为参数传递给生成器函数。
function* logGenerator() {
console.log(0);
console.log(1, yield);
console.log(2, yield);
console.log(3, yield);
}
var gen = logGenerator();
// the first call of next executes from the start of the function
// until the first yield statement
gen.next(); // 0
gen.next('pretzel'); // 1 pretzel
gen.next('california'); // 2 california
gen.next('mayonnaise'); // 3 mayonnaise
而且func.bind(window, arg)
只是用参数来称呼它的奇特方式,
wherebind
方法返回一个函数...您可以this
像window
在该函数中一样访问...
例子:
function func(args) {
console.log(this, args)
}
func.bind("this", "args")();
推荐阅读
- swift - AudioKit 实时输出 PCM 数据
- javascript - 将 JavaScript 解密重构为 C# - AES 128
- fabricjs - 是否可以使用 fabric.js 创建表格画布元素?
- java - 由 maven 插件打包的 Spring Boot 应用程序不工作,在 IDE 中工作
- python - 如何使用来自另一个数据帧的值更新 Pandas 数据帧和一个新列?
- flutter - Webview不断刷新——Flutter InAppWebView
- javascript - 学习 Socket.io - 在模块和服务器之间传递数据
- reactjs - 加载动画未在 React 中渲染
- python - 带线程的基本 Mapreduce 比顺序版本慢
- java - sqlite 数据库中未正确返回主键