Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
105 views
in Technique[技术] by (71.8m points)

javascript - JavaScript中的“功能*”是什么?(What is “function*” in JavaScript?)

In this page I found a new JavaScript function type:(在页面中,我找到了新的JavaScript函数类型:)

// 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.(我已经知道yieldlet[?,?]=[?,?]做什么 ,但不知道该function*是什么意思。)

What is it?(它是什么?)

PS don't bother trying Google, it's impossible to search for expressions with asterisks ( they're used as placeholders ).(PS不会尝试使用Google,因为搜索带有星号的表达式是不可能的它们被用作占位符 )。)

  ask by string QNA translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's a Generator function.(这是一个生成器功能。)

Generators are functions which can be exited and later re-entered.(生成器是可以退出并随后重新输入的函数。)

Their context (variable bindings) will be saved across re-entrances.(它们的上下文(变量绑定)将在重新进入时保存。)

Calling a generator function does not execute its body immediately;(调用生成器函数不会立即执行其主体。)

an iterator object for the function is returned instead.(而是返回该函数的迭代器对象。) When the iterator's next() method is called, the generator function's body is executed until the first yield expression, which specifies the value to be returned from the iterator or, with yield* , delegates to another generator function.(调用迭代器的next()方法时,将执行生成器函数的主体,直到第一个yield表达式(该表达式指定要从迭代器返回的值)或使用yield*委托给另一个生成器函数为止。)

Historical note:(历史记录:)

It's a proposed syntax for EcmaScript.next .(这是EcmaScript.next的建议语法。)

Dave Herman of Mozilla gave a talk about EcmaScript.next .(Mozilla的Dave Herman谈到了EcmaScript.next 。)

At 30:15 he talks about generators.(在30:15他谈到了发电机。)

Earlier, he explains how Mozilla is experimentally implementing proposed language changes to help steer the committee.(之前,他解释了Mozilla如何通过实验方式实施建议的语言更改以帮助指导委员会。)

Dave works closely with Brendan Eich, Mozilla's CTO (I think), and the original JavaScript designer.(Dave与Mozilla的CTO(我认为)和原始JavaScript设计师Brendan Eich紧密合作。)

You can find more detail on the EcmaScript working group wiki: http://wiki.ecmascript.org/doku.php?id=harmony:generators(您可以在EcmaScript工作组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.(工作组(TC-39)普遍同意EcmaScript.next应该具有某种生成器迭代器建议,但这不是最终的。)

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 (ie, function activations).(一流的协程,表示为封装暂停执行上下文(即函数激活)的对象。)

Prior art: Python, Icon, Lua, Scheme, Smalltalk.(现有技术:Python,Icon,Lua,Scheme,Smalltalk。)

Examples(例子)

The “infinite” sequence of Fibonacci numbers (notwithstanding behavior around 2 53 ):(斐波纳契数的“无限”序列(尽管行为在2 53附近):)

 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 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...