Skip to content

Instantly share code, notes, and snippets.

@lili21
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lili21/1e2821e56d3ca5dc17d7 to your computer and use it in GitHub Desktop.
Save lili21/1e2821e56d3ca5dc17d7 to your computer and use it in GitHub Desktop.
extend deep ,
function type(source) {
return Object.prototype.toString.call(source).replace(/^\[object (.+)\]/g, '$1').toLowerCase();
}
function forEach(source, callback) {
var type = type(source)
if (type === 'array')
for (var i = 0, l = source.length;i < l;i++) {
callback(source[i], i);
}
}
else if (type === 'object') {
for (var k in source) {
source.hasOwnProperty(k) && callback(source[k], k);
}
} else {
return undefined;
}
}
function extend() {
var args = Array.prototype.slice.call(arguments),
result = args.shift(),
source = args.shift();
forEach(source, function(v, k) {
//console.log(k);
if (type(v) === 'object') {
result[k] = result[k] || {};
extend.call(this, result[k], v);
} else {
result[k] = v;
}
});
return args.length > 0 ? extend.apply(this, (args.unshift(result), args)) : result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment