Skip to content

Instantly share code, notes, and snippets.

@erayarslan
Created August 24, 2017 08:54
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 erayarslan/a77f7126d47c9befb3ea3457cb713561 to your computer and use it in GitHub Desktop.
Save erayarslan/a77f7126d47c9befb3ea3457cb713561 to your computer and use it in GitHub Desktop.
dolly.js
//
// deep object cloning with pure javascript (jquery "extend" function based)
//
// full documantation : http://api.jquery.com/jquery.extend/
//
var dolly = function() {
var options, name, src, copy, copyIsArray, clone, target = arguments[0] || {}, i = 1, length = arguments.length, deep = false;
if (typeof target === "boolean") {
deep = target; target = arguments[ i ] || {}; i++;
}
if (typeof target !== "object" && !this.isFunction(target)) {
target = {};
}
if (i === length) {
target = this; i--;
}
for (; i < length; i++) {
if ((options = arguments[ i ]) !== null) {
for (name in options) {
src = target[name];
copy = options[name];
if (target === copy) {
continue;
}
if (deep && copy && ( (this.isPlainObject(copy)) ||
(copyIsArray = (this.isArray(copy))) )) {
if (copyIsArray) {
copyIsArray = false;
clone = src && (this.isArray(src)) ? src : [];
} else {
clone = src && (this.isPlainObject(src)) ? src : {};
}
target[name] = this.dolly( deep, clone, copy );
} else if (copy !== undefined) {
target[name] = copy;
}
}
}
} return target;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment