什么是“功能*”在JavaScript? [英] What is "function*" in JavaScript?

查看:89
本文介绍了什么是“功能*”在JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

页面中,我发现了一个新的JavaScript功能类型:

  //注意:Firefox中不支持function * 
//删除星号以使此代码在Firefox 13中正常工作

function * fibonacci(){// !!!这是有趣的线!
let [prev,curr] = [0,1]; (;;)的
{
[prev,curr] = [curr,prev + curr];
yield curr;
}
}

我已经知道什么 yield let [?,?] = [?,?] do ,但不知道 function *

不要打扰Google,这是不可能使用星号搜索表达式(它们被用作占位符

解决方案

这是 EcmaScript.next 的提议语法。 p>

Mozilla的Dave Herman发表了一个关于 EcmaScript.next 。在 30:15 他谈论发电机。



此前,他解释了Mozilla如何实验性地实施提出的语言变更,以帮助指导委员会。 Dave与Mozilla的首席技术官Brendan Eich(我想)和原来的JavaScript设计师紧密合作。



您可以在EcmaScript工作组wiki上找到更多的细节: http://wiki.ecmascript.org/doku.php?id=harmony:generators



工作组(TC-39)普遍认为EcmaScript.next应该具有某种生成器迭代器提案,但这不是最终的。

您不应该依靠这种显示,而不会在下一版本的语言中进行更改,即使没有更改,也可能不会在其他浏览器中广泛显示一段时间。


概述



表示为对象的一级协同程序封装了暂停的执行上下文(即函数激活)。现有技术:Python,Icon,Lua,Scheme,Smalltalk。



示例



无限斐波纳契数字(尽管行为大约2 53 ):

  function * fibonacci(){
let [prev,curr] = [0,1]; (;;)的
{
[prev,curr] = [curr,prev + curr];
yield curr;
}
}

生成器可以循环遍历:

  for(n of fibonacci()){
// truncate the sequence at 1000
if(n> 1000)
break;
print(n);
}

生成器是迭代器:

  let seq = fibonacci(); 
print(seq.next()); // 1
print(seq.next()); // 2
print(seq.next()); // 3
print(seq.next()); // 5
print(seq.next()); // 8



In this page I found a new JavaScript function type:

// NOTE: "function*" is not supported yet in Firefox.
// Remove the asterisk in order for this code to work in Firefox 13 

function* fibonacci() { // !!! this is the interesting line !!!
    let [prev, curr] = [0, 1];
    for (;;) {
        [prev, curr] = [curr, prev + curr];
        yield curr;
    }
}

I already know what yield, let and [?,?]=[?,?] do, but have no idea what the function* is meant to be. What is it?

P.S. don't bother trying Google, it's impossible to search for expressions with asterisks (they're used as placeholders).

解决方案

It's a proposed syntax for EcmaScript.next.

Dave Herman of Mozilla gave a talk about EcmaScript.next. At 30:15 he talks about generators.

Earlier, he explains how Mozilla is experimentally implementing proposed language changes to help steer the committee. Dave works closely with Brendan Eich, Mozilla's CTO (I think), and the original JavaScript designer.

You can find more detail on the EcmaScript working group wiki: http://wiki.ecmascript.org/doku.php?id=harmony:generators

The working group (TC-39) has general agreement that EcmaScript.next should have some kind of generator iterator proposal, but this is not final.

You shouldn't rely on this showing up without changes in the next version of the language, and even if it doesn't change, it probably won't show up widely in other browsers for a while.

Overview

First-class coroutines, represented as objects encapsulating suspended execution contexts (i.e., function activations). Prior art: Python, Icon, Lua, Scheme, Smalltalk.

Examples

The "infinite" sequence of Fibonacci numbers (notwithstanding behavior around 253):

function* fibonacci() {
    let [prev, curr] = [0, 1];
    for (;;) {
        [prev, curr] = [curr, prev + curr];
        yield curr;
    }
}

Generators can be iterated over in loops:

for (n of fibonacci()) {
    // truncate the sequence at 1000
    if (n > 1000)
        break;
    print(n);
}

Generators are iterators:

let seq = fibonacci();
print(seq.next()); // 1
print(seq.next()); // 2
print(seq.next()); // 3
print(seq.next()); // 5
print(seq.next()); // 8

这篇关于什么是“功能*”在JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆