This file contains 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 createIterator(items) { | |
var i = 0; | |
return { | |
next: function() { | |
// NZakas' way is really straight forward what 'done' and 'value' is. | |
var done = (i >= items.length); | |
var value = done ? undefined : items[i++]; | |
return { | |
done: done, |
This file contains 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
let promise = Promise.resolve('hello'); | |
promise.then(function(data) { | |
console.log(data); | |
}); | |
// Or: ------------------------ | |
let promise = Promise.reject('oops'); |
This file contains 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
var fs = require('fs'); | |
function readFile(filename) { | |
return new Promise(function(resolve, reject) { | |
fs.readFile(filename, {encoding: 'utf8'}, function(err, data) { | |
if (err) { | |
reject(err); | |
return; | |
} | |
resolve(data); |
This file contains 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 add(getX, getY, cb) { | |
var x, y; | |
getX(function(xVal) { | |
x = xVal; | |
if (y !== undefined) { | |
cb(x + y); | |
} | |
}); | |
get(function(yVal) { |
This file contains 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
// cause safari private mode do not support storage | |
if (typeof localStorage === 'object') { | |
try { | |
localStorage.setItem('localStorage', 1); | |
localStorage.removeItem('localStorage'); | |
} catch (e) { | |
Storage.prototype._setItem = Storage.prototype.setItem; | |
Storage.prototype.setItem = function() {}; | |
alert('你的浏览器似乎不支持本地缓存,如果你是Safari用户,请关闭“无痕浏览”模式'); |
This file contains 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
.DS_Store | |
*.log | |
dist | |
node_modules | |
.sass-cache | |
.svn | |
.gitmodules |
This file contains 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
// pre es5 | |
function isEmpty(myObject) { | |
for (var key in myObject) { | |
if (myObject.hasOwnProperty(key)) { | |
return false | |
} | |
} | |
return true | |
} |
This file contains 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
// accept a array of reducers | |
const combineReducers = (reducers) => { | |
// returns a huge reducer, looks like below gist | |
return (state = {}, action) => { | |
return Object.keys(reducers).reduce( | |
(nextState, key) => { | |
nextState[key] = reducers[key]( | |
state[key], | |
action | |
) |
This file contains 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 createStore = (reducer) => { | |
// only this place create the one and only state | |
let state | |
let listeners = [] | |
const getState = () => state | |
const dispatch = (action) => { | |
// change state only by dispatching an action | |
state = reducer(state, action) |
This file contains 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] = [0, 1]; | |
// using deconstruct feature implement reducer, really cool... | |
for (;;) { | |
[prev, curr] = [curr, curr + prev]; | |
yield curr; | |
} | |
} |