node-es6-examples
node-es6-examples copied to clipboard
Array pattern destructuring not actually supported?
When demonstrating generators:
function* fibonacci() {
let a = 0, b = 1;
while(true) {
yield a;
// this syntax doesnt appear to be actually supported
[a, b] = [b, a + b];
}
}
ReferenceError: Invalid left-hand side in assignment
I'm using node v0.11.13 with the --harmony flag.
Sorry, I took another look at the document and saw at the bottom:
"We look forward for tasty syntax sugar yet to come, such as rest and spread operators, destructuring "...
No no, this is good. Thanks for posting. :) All examples should run in the latest Node.
No problem then, I've been playing with harmony a lot lately and this resource is excellent. Though this feature is not currently present, it would appear that it is inbound in v8
Hi Justin, there is a mistake in given Fibonacci example. If n is 0 it is 0 (e.g. Ron Knott's Fib table).
I share my code at jsbin.com for ES5
// iterative
function fibonacci (n) {
var i = 0,
temp = [i, i + 1];
for (i = 2; i <= n; i += 1) {
temp[i] = temp[i - 1] + temp[i - 2];
}
return (temp[n]);
}
// recursive one liner (very slow if n > 50 ! & bad to read)
function f (n) {
return ((n >= 2) ? f (n - 1) + f (n - 2) : n);
}
@ronny-springer perhaps you should create a seperate issue for that so that it gets noticed. This issue has to do with destructuring syntax in general and not the fibonacci example per se...