Reputation: 3654
I was looking at the mdn javascript reference and noticed that yield
is listed in the operators section. On the other hand return
is listed as a statement. I also found yield
has a operator precedence of 2.
What features of yield
make it fall into the operator class rather than a statement? Why does return
fall into statements rather than operators?
Upvotes: 5
Views: 66
Reputation: 386600
It is an operator because it can be used in an expression.
function* g() {
value = 3;
while (value !== 5) value = Math.floor(yield value + 1);
}
var gen = g();
console.log(gen.next().value);
console.log(gen.next(1.5).value);
Upvotes: 3
Reputation: 6888
I am not certain on this, but in the context of a generator yield
sends data to the generator.next()
in that way it operates much like a function. Operators are special classes of functions in the most languages (JavaScript inlcuded).
You could almost imagine the generator.next
calling into its instance passing a callback as to where to resume. And yield
invoking that callback
Return
signals the end of a path of execution and to replace a return value into the proper memory location and unwind the call stack by 1 unit. If feels primordial to the definition of the language,
Upvotes: 1