Skip to content

Instantly share code, notes, and snippets.

@think49
Last active August 23, 2017 08:28
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 think49/96e8fc064b917287cadb1dfacc39695c to your computer and use it in GitHub Desktop.
Save think49/96e8fc064b917287cadb1dfacc39695c to your computer and use it in GitHub Desktop.
deep-copy-object.js: オブジェクトをディープコピー
/**
* 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