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
| // ugly way | |
| var obj = { | |
| a: 1, | |
| b: 2, | |
| c: 3, | |
| [Symbol.iterator]() { | |
| var keys = Object.keys(this); | |
| var idx = 0; | |
| return { |
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 lowercase(v) { | |
| return v.toLowerCase(); | |
| } | |
| function uppercase(v) { | |
| return v.toUpperCase(); | |
| } | |
| var words = ["Now","Is","The","Time"]; | |
| var moreWords = ["The","Quick","Brown","Fox"]; |
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 gatherArgs(fn) { | |
| return function gatheredFn(...argsArr) { | |
| return fn( argsArr ); | |
| }; | |
| } | |
| function gatherArgProps(fn,propOrder = []) { | |
| return function gatheredFn(...args) { | |
| return fn( | |
| zip( propOrder, args ) |
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 curryProps(fn,arity = 1) { | |
| return (function nextCurried(prevArgs){ | |
| return function curried(nextArg){ | |
| if (!nextArg || typeof nextArg != "object") { | |
| nextArg = { value: nextArg }; | |
| } | |
| var [key] = Object.keys( nextArg ); | |
| var args = Object.assign( {}, prevArgs, { [key]: nextArg[key] } ); |
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 *zipIterators(...its) { | |
| its = its.map(it => (!it.next && it[Symbol.iterator]) ? it[Symbol.iterator]() : it); | |
| while (its.length > 0) { | |
| let entry = []; | |
| let itsCopy = [...its]; | |
| let it; | |
| while (it = itsCopy.shift()) { | |
| its.shift(); | |
| let res = it.next(); |
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
| Object.defineProperty(Number.prototype,Symbol.iterator,{ | |
| *value({ start = 0, step = 1 } = {}) { | |
| var inc = this > 0 ? step : -step; | |
| for (let i = start; Math.abs(i) <= Math.abs(this); i += inc) { | |
| yield i; | |
| } | |
| }, | |
| enumerable: false, | |
| writable: true, | |
| configurable: 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
| Object.defineProperty(Object.prototype,Symbol.toStringTag,{ | |
| get() { | |
| return JSON.stringify(this); | |
| }, | |
| set(v) { | |
| Object.defineProperty(Object.prototype,Symbol.toStringTag,{ | |
| value: v, | |
| enumerable: false, | |
| writable: true, | |
| configurable: 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
| // Doesn't throw an error, but doesn't return an object | |
| const getObj = () => { a: 1 }; | |
| console.log(getObj()); //=> val 'undefined' : void | |
| // Reason: It looks like an arrow function returning an object, but it isn't. | |
| // It's an arrow function with a function body (containing a labeled | |
| // statement) that returns nothing. | |
| const getObj = () => | |
| { // <- Start function body | |
| a: // <- A label to use when break/continue; e.g. break a; |
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
| class Foo { | |
| one() { | |
| return this.x; | |
| } | |
| } | |
| class Bar extends Foo { | |
| two() { | |
| return this.y; | |
| } |
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
| // EAT: Eager Async Thunk, aka "promise" | |
| function gimmeFutureVal(x) { | |
| var fs = [], v; | |
| doSomethingAsync(x,function res(r){ | |
| if (fs.length > 0) { | |
| v = r; | |
| while (fs.length > 0) { | |
| fs.shift()(v); |