4 ms·
> fat arrow: (a) => a * a is the same as function(a) { return a * a; } No, it's equivalent to the following: (function (a) { return a * a; }).bind(this); T
by TazeTSchnitzel 12y ago
> fat arrow: (a) => a * a is the same as
function(a) { return a * a; }
No, it's equivalent to the following:
(function (a) { return a * a; }).bind(this);
The fat arrow has lexically scoped this.
On an entirely different note, I never liked how generators are marked with an asterisk. A generator isn't special, it is just a function returning an iterator. A generator should be able to return normally as well as yield.
- espadrine 12y agoYou're absolutely right! Fixed, thanks! (PS, repository is here: https://github.com/espadrine/New-In-A-Spec https://github.com/espadrine/New-In-A-Spec)
- pimterry 12y agoGenerators have to be marked as an asterisk as otherwise there's ambiguity; until recently 'yield' wasn't a reserved word, so it might be a variable name or anything. Without disambiguating (via the *) old JavaScript can potentially break.
- untothebreach 12y ago'yield' was a reserved word in the ES4 spec, and a 'FutureReservedWord' in ES5 so if there is code that uses it as an identifier, it has been technically broken since 2008...
- gsnedders 12y agoWhat ES4 says is irrelevant. yield is only a FutureReservedWord in ES5 when within strict mode code; in non-strict code it is not a keyword, and plenty of code uses it and works in ES5 implementations.
- ptx 12y ago> A generator isn't special, it is just a function returning an iterator. A generator should be able to return normally as well as yield. But in a generator function the iterator is returned immediately, before any of the code in the function runs, so how would it return anything else? For example: function* f() { console.log("function commencing!"); for (x of [1, 2, 3]) { console.log("yielding " + x); yield x; } } var it = f(); console.log("iterator returned from function:", it); console.log(it.next().value); would print: "iterator returned from function:" Generator { } Scratchpad/1:19 "function commencing!" Scratchpad/1:11 "yielding 1" Scratchpad/1:13 1 So even if the very first thing the function does is if (isTuesday()) return something; else yield somethingElse; ...by that point it would have already returned an iterator, so it would be too late to decide otherwise, wouldn't it?
- TazeTSchnitzel 12y ago>But in a generator function the iterator is returned immediately, before any of the code in the function runs, so how would it return anything else? Oh, that's interesting, I didn't know ES6 implemented generators that way. That's really weird. EDIT: Looks like PHP does the same thing. Hmm, I can see the merits of an asterisk, then.
- ptx 12y agoPython works the same way, so I just assumed ES6 did as well and tested it in Firefox. :)