Skip to content

Instantly share code, notes, and snippets.

@zhuzhuaicoding
Created July 20, 2015 07:28
Show Gist options
  • Save zhuzhuaicoding/49cfced2ae129a362161 to your computer and use it in GitHub Desktop.
Save zhuzhuaicoding/49cfced2ae129a362161 to your computer and use it in GitHub Desktop.
merge from highcharts
function merge() {
var i,
args = arguments,
len,
ret = {},
doCopy = function (copy, original) {
var value, key;
// An object is replacing a primitive
if (typeof copy !== 'object') {
copy = {};
}
for (key in original) {
if (original.hasOwnProperty(key)) {
value = original[key];
// Copy the contents of objects, but not arrays or DOM nodes
if (value && typeof value === 'object' && Object.prototype.toString.call(value) !== '[object Array]' &&
key !== 'renderTo' && typeof value.nodeType !== 'number') {
copy[key] = doCopy(copy[key] || {}, value);
// Primitives and arrays are copied over directly
} else {
copy[key] = original[key];
}
}
}
return copy;
};
// If first argument is true, copy into the existing object. Used in setOptions.
if (args[0] === true) {
ret = args[1];
args = Array.prototype.slice.call(args, 2);
}
// For each argument, extend the return
len = args.length;
for (i = 0; i < len; i++) {
ret = doCopy(ret, args[i]);
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment