Created
August 24, 2017 08:54
-
-
Save erayarslan/a77f7126d47c9befb3ea3457cb713561 to your computer and use it in GitHub Desktop.
dolly.js
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
// | |
// 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