Skip to content

Instantly share code, notes, and snippets.

View ZhihaoLau's full-sized avatar
💭
Meh

lzo ZhihaoLau

💭
Meh
View GitHub Profile
@ZhihaoLau
ZhihaoLau / printObj.js
Last active January 30, 2016 16:17
Print a JavaScript object
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(', '); + ' }';
}
@ZhihaoLau
ZhihaoLau / log.js
Created January 30, 2016 16:24
Log
function log(obj) {
console.log(obj);
}
@ZhihaoLau
ZhihaoLau / connect.js
Created May 17, 2016 04:28 — forked from gaearon/connect.js
connect.js explained
// 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 (
@ZhihaoLau
ZhihaoLau / cool_generator.js
Last active June 1, 2016 14:32
[ES6] Using generator with for ... of statement
function* fibonacci() {
let [prev, curr] = [0, 1];
// using deconstruct feature implement reducer, really cool...
for (;;) {
[prev, curr] = [curr, curr + prev];
yield curr;
}
}
@ZhihaoLau
ZhihaoLau / createStore.js
Created June 3, 2016 17:22
implement createStore from scratch
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)
@ZhihaoLau
ZhihaoLau / combineReducers.js
Created June 5, 2016 07:44
implement Redux's combineReducers from scratch
// 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
)
@ZhihaoLau
ZhihaoLau / isEmpty.js
Created July 3, 2016 04:50
Check if object is empty
// pre es5
function isEmpty(myObject) {
for (var key in myObject) {
if (myObject.hasOwnProperty(key)) {
return false
}
}
return true
}
@ZhihaoLau
ZhihaoLau / .gitignore
Last active January 20, 2017 09:55
generic .gitignore configuration
.DS_Store
*.log
dist
node_modules
.sass-cache
.svn
.gitmodules
@ZhihaoLau
ZhihaoLau / safari_private_mode_isssue.js
Last active August 15, 2016 03:52
safari无痕浏览模式下,setItem抛出异常导致程序不能正常运行
// 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用户,请关闭“无痕浏览”模式');
@ZhihaoLau
ZhihaoLau / AsyncSum.js
Created September 7, 2016 03:01
AsyncSum.js
function add(getX, getY, cb) {
var x, y;
getX(function(xVal) {
x = xVal;
if (y !== undefined) {
cb(x + y);
}
});
get(function(yVal) {