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
// Use Gists to store code you would like to remember later on | |
console.log(window); // log the "window" object to the console |
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
In JavaScript, undefined means a variable has been declared but has not yet been assigned a value, such as: | |
var TestVar; | |
alert(TestVar); //shows undefined | |
alert(typeof TestVar); //shows undefined | |
null is an assignment value. It can be assigned to a variable as a representation of no value: | |
var TestVar = null; | |
alert(TestVar); //shows null | |
alert(typeof TestVar); //shows object |
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
// this assumes that you have jquery, or any ajax utils like `isomorphic-fetch` | |
function storeDataCb(username, password) { | |
if (!window.localStorage) return | |
localStorage.username = username | |
localStorage.password = password | |
} | |
function logIn() { |
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
/* | |
* I got this inspiration from the source code of redux. | |
* Essentially, compose(f, g, h) equals to (arg) => f(g(h(arg))) | |
*/ | |
function compose(...funcs) { | |
if (!funcs.length) return (arg) => arg | |
if (funcs.length === 1) return funcs[0] | |
return funcs.reduce((a, b) => (...args) => a(b(...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
// https://medium.com/gitconnected/understand-javascript-promises-by-building-a-promise-from-scratch-84c0fd855720 | |
class SimplePromise { | |
constructor(executionFunction) { | |
this.promiseChain = []; | |
this.handleError = () => {}; | |
this.onResolve = this.onResolve.bind(this); | |
this.onReject = this.onReject.bind(this); | |
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 optimizeCb(func, context, argCount) { | |
if (!context) return func | |
switch (argCount ? argCount : 3) { | |
case 1: return function(val) { | |
return func.call(context, val) | |
} | |
case 2: return function(val, other) { | |
return func.call(context, val, other) | |
} |
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
// a code snippets from underscore.js for type checking in JavaScript | |
_.each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp', 'Error'], function(name) { | |
_['is' + name] = function(obj) { | |
return toString.call(obj) === '[object ' + name + ']'; | |
}; | |
}); | |
function isObject(item) { | |
const type = typeof item |
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
// in the namespace of `_` | |
// The simplest extend | |
function simpleExtend(obj, src) { | |
for (let key in src) { | |
obj[key] = src[key] | |
} | |
return obj | |
} | |
// the two different type of configuration of key assigners |
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 promisify(fn) { | |
return function(...args) { | |
return new Promise((res, rej) => { | |
fn(...args, function() { | |
res(arguments) | |
}) | |
}) | |
} | |
} |
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 async(makeGenerator) { | |
return (...args) => { | |
var generator = makeGenerator.apply(this, args); | |
function next(result) { | |
// result => { done: [Boolean], value: [Object] } | |
if (result.done) return Promise.resolve(result.value); | |
return Promise.resolve(result.value).then((res) => { | |
return next(generator.next(res.value)); |
OlderNewer