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 printObj = typeof JSON != 'undefined' ? JSON.stringify : function(obj) { | |
var arr = []; | |
$.each(obj, function(key, val) { | |
var next = key + ': '; | |
next += $.isPlainObject(val) ? printObj(val) : val; | |
arr.push(next); | |
}); | |
return '{ ' + arr.join(', '); + ' }'; | |
} |
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 log(obj) { | |
console.log(obj); | |
} |
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
// connect() is a function that injects Redux-related props into your component. | |
// You can inject data and callbacks that change that data by dispatching actions. | |
function connect(mapStateToProps, mapDispatchToProps) { | |
// It lets us inject component as the last step so people can use it as a decorator. | |
// Generally you don't need to worry about it. | |
return function (WrappedComponent) { | |
// It returns a component | |
return class extends React.Component { | |
render() { | |
return ( |
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; | |
} | |
} |
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
// 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
// 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
.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
// 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
function add(getX, getY, cb) { | |
var x, y; | |
getX(function(xVal) { | |
x = xVal; | |
if (y !== undefined) { | |
cb(x + y); | |
} | |
}); | |
get(function(yVal) { |
OlderNewer