admin管理员组文章数量:1134247
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).
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).
Share Improve this question edited Apr 25, 2014 at 6:00 thefourtheye 239k53 gold badges464 silver badges500 bronze badges asked Mar 8, 2012 at 15:53 string QNAstring QNA 3,1903 gold badges18 silver badges14 bronze badges 7 | Show 2 more comments3 Answers
Reset to default 232It'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 firstyield
expression, which specifies the value to be returned from the iterator or, withyield*
, delegates to another generator function.
Historical note:
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
It's a generator function - and it said so in the page you cite, in the comment you replaced with "this is the interesting line"...
Basically it's a way to specify sequences programmatically so that they can be passed around and elements accessed by index without having to compute the entire sequence (possibly infinite in size) beforehand.
The function*
type looks like it acts as a generator function for processes that can be iterated.
C# has a feature like this using "yield return" see 1 and see 2
Essentially this returns each value one by one to whatever is iterating this function, which is why their use case shows it in a foreach style loop.
本文标签: What is quotfunction*quot in JavaScriptStack Overflow
版权声明:本文标题:What is "function*" in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736803966a1953609.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
function*
syntax is supported in Firefox since v26: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…* . Older versions used a different syntax. – Nickolay Commented Sep 9, 2014 at 22:16*
was stripped from the link from @Nickolay. Here's a link directly tofunction*
at MDN. Sure enough, "basic" support since v26. – ruffin Commented Apr 29, 2015 at 17:16