Last active
August 29, 2015 14:07
-
-
Save lili21/1e2821e56d3ca5dc17d7 to your computer and use it in GitHub Desktop.
extend deep ,
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 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