在我看来,在ES6,下面的两个功能都非常接近相同的:
function () {
return this;
}.bind(this);
() => {
return this;
};
Run Code Online (Sandbox Code Playgroud)
最终结果看起来是一样的:箭头函数生成一个JavaScript函数对象,其this上下文绑定到与this创建它们的位置相同的值.
显然,在一般意义上,Function.prototype.bind它比箭头函数更灵活:它可以绑定到本地以外的值this,并且它可以this在任何时间点绑定任何函数,可能在最初创建之后很长时间.不过,我不问如何bind本身就是从箭头的功能不同,我问箭头的功能是如何从立即调用不同bind使用this.
ES6中的两个结构之间是否有任何差异?
我的问题是:鉴于提供的(非常简单的)生成器,是否可以将生成器恢复到其原始状态以便再次使用?
var generator = function*() {
yield 1;
yield 2;
yield 3;
};
var iterable = generator();
for (let x of iterable) {
console.log(x);
}
// At this point, iterable is consumed.
// Is there a method for moving iterable back
// to the start point by only without re-calling generator(),
// (or possibly by re-calling generator(), only by using prototype
// or constructor methods available within the iterable object)
// so the following code would work again?
for (let …Run Code Online (Sandbox Code Playgroud) 根据Eric Lippert的说法,匿名迭代器没有被添加到语言中,因为实现它会过于复杂.
这对我来说没关系,在他们继续实施匿名异步方法之前,它并没有打扰我.编译器必须对异步方法执行与迭代器相同的操作(将它们转换为状态机),所以我很困惑为什么匿名迭代器也不允许,当匿名异步方法时.
有人可以对此有所了解吗?