ES6 - for loops

Julio Cesar Ody
2013-05-03T04:42:31Z

for-loops.js

| // ES6 for loops
| // =============
|
| // Things in ES6 can be "iterable". Arrays are iterable by default.
|
| var fruits = ['Apple', 'Banana', 'Grape'];
|
| for (var fruit of fruits)
| console.log('Fruit: ' + fruit);
|
| // => "Fruit: Apple"
| // => "Fruit: Banana"
| // => "Fruit: Grape"
|
| // Objects apparently not iterable as anyone in the planet would expect.
| // You need to declare an iterator which is a method that defines how
| // each property is return.
| // Y U NO USE A DEFAULT ITERATOR?!
|
| var person = { name: 'Joe', age: 25, bio: 'Long bio here' };
|
| var attributes = function(person) {
| for (var attr in person) yield [attr, person[attr] ]
| };
|
| for (var [attribute, value] of attributes(person))
| console.log(attribute + ': ' + value);