Last active
August 23, 2017 08:28
-
-
Save think49/96e8fc064b917287cadb1dfacc39695c to your computer and use it in GitHub Desktop.
deep-copy-object.js: オブジェクトをディープコピー
This file contains hidden or 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-copy-object.js | |
* | |
* Deep copy objects. | |
* | |
* @version 1.0.0 | |
* @author think49 | |
* @url https://gist.github.com/think49/96e8fc064b917287cadb1dfacc39695c | |
* @license http://www.opensource.org/licenses/mit-license.php (The MIT License) | |
*/ | |
var deepCopyObject = (function (Object, keys) { | |
'use strict'; | |
return function deepCopyObject (target, source /* [,... source] */) { | |
if (typeof target === 'undefined' || target === null) { | |
throw new TypeError('Cannot convert undefined or null to object'); | |
} | |
var len = arguments.length, i = 1; | |
target = Object(target); | |
while (i < len) { | |
source = arguments[i++]; | |
if (typeof source !== 'undefined' && source !== null) { | |
source = Object(source); | |
for (var j = 0, sKeys = keys(source), sLen = sKeys.length, key, value; j < sLen; ++j) { | |
key = sKeys[j]; | |
value = source[key]; | |
target[key] = Object(value) === value ? deepCopyObject(value) : value; | |
} | |
} | |
} | |
return target; | |
}; | |
}(Object, Object.keys)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment