Last active
July 3, 2018 09:53
-
-
Save langjt/47ac3e1509088f0901d0 to your computer and use it in GitHub Desktop.
sample extend
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
var lang = function(){}; | |
lang.extend = lang.prototype.extend = function(){ | |
var len = arguments.length, | |
target = {}, | |
options, name, src, copy, clone, i= 0; | |
if (len === 1) { | |
target = this; | |
} else if(len > 1) { | |
target = arguments[0]; | |
i = 1; | |
} | |
for (; i < len; i++) { | |
options = arguments[i]; | |
for (name in options) { | |
src = target[name]; | |
copy = options[name]; | |
if (target == copy) { | |
continue; | |
} | |
if (typeof copy === 'object' || Array.isArray(copy)) { | |
if (Array.isArray(copy)) { | |
clone = Array.isArray(src) ? src : []; | |
} else { | |
clone = (typeof src === 'object') ? src : {}; | |
} | |
target[name] = lang.extend.call(target, 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