This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ES6 | |
function* foo(){ | |
yield bar(); | |
} | |
// ES5 Compiled | |
"use strict"; | |
var _marked = /*#__PURE__*/ regeneratorRuntime.mark(foo); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ES7 | |
async function foo() { | |
await bar(); | |
} | |
// ES5 complied | |
let foo = (() => { | |
var _ref = _asyncToGenerator(function*() { | |
yield bar(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* fibonacci() { | |
let [prev, curr] = [1, 1]; | |
while (true) { | |
[prev, curr] = [curr, prev + curr]; | |
yield curr; | |
} | |
} | |
for (let n of fibonacci()) { | |
console.log(n); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* idMaker(){ | |
var index = 0; | |
while(index < 3) | |
yield index++; | |
} | |
const gen = idMaker(); | |
console.log(gen.next().value); // 0 | |
console.log(gen.next().value); // 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const arr = [...Array(100).keys()].map(i => 0); | |
console.log(arr); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const arr = []; | |
for(let i=0; i<100; i++) { | |
arr.push(0); | |
} | |
console.log(arr); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const iterable = { | |
[Symbol.iterator]() { | |
return { | |
i: 0, | |
next() { | |
if (this.i < 3) { | |
return { value: this.i++, done: false }; | |
} | |
return { value: undefined, done: true }; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function makeRangeIterator(start = 0, end = Infinity, step = 1) { | |
var nextIndex = start; | |
var n = 0; | |
var rangeIterator = { | |
next: function() { | |
var result; | |
if (nextIndex < end) { | |
result = { value: nextIndex, done: false } | |
} else if (nextIndex == end) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* array */ | |
let iterable = [10, 20, 30]; | |
for (let value of iterable) { | |
console.log(value); // 10 20 30 | |
} | |
/* string */ | |
let iterable = "boo"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
renderHtmlTag = tagName => content => `<${tagName}>${content}</${tagName}>` | |
renderDiv = renderHtmlTag('div') | |
renderH1 = renderHtmlTag('h1') | |
console.log( | |
renderDiv('this is a really cool div'), | |
renderH1('and this is an even cooler h1') | |
) |
NewerOlder