Skip to content

Instantly share code, notes, and snippets.

@husa
Last active August 29, 2015 14:25
Show Gist options
  • Save husa/75097fbadc0a6a6b1b84 to your computer and use it in GitHub Desktop.
Save husa/75097fbadc0a6a6b1b84 to your computer and use it in GitHub Desktop.
extend. the one and only
function extend () {
return [].reduce.call(arguments, function (prev, current) {
for (var prop in current) {
if (!current.hasOwnProperty(prop)) continue;
if (/^(object|array)$/.test(type(current[prop])) && current[prop] !== current) {
if (!/^(object|array)$/.test(type(prev[prop]))) {
prev[prop] = type(current[prop]) === 'array' ? [] : {};
}
prev[prop] = extend(prev[prop], current[prop]);
} else {
prev[prop] = current[prop];
}
}
return prev;
}, {});
}
function type(o) {return Object.prototype.toString.call(o).match(/\[object (.*?)\]/)[1].toLowerCase();}
@husa
Copy link
Author

husa commented Jul 16, 2015

To be used with type.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment