Last active
August 23, 2017 08:01
-
-
Save think49/a615978e85f44cb0ef5c7cbdd3cca942 to your computer and use it in GitHub Desktop.
Object.assign(): オブジェクトをシャローコピー(ES6 Polyfill)
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
/** | |
* es6-object-assign.js | |
* | |
* Object.assign( target, ...sources ) | |
* The assign function is used to copy the values of all of the enumerable own properties from one or more source objects to a target object. When the assign function is called, the following steps are taken. (ECMA-262 6th Edition / ECMAScript 2015) | |
* | |
* @version 1.0.0 | |
* @author think49 | |
* @url https://gist.github.com/think49/a615978e85f44cb0ef5c7cbdd3cca942 | |
* @license http://www.opensource.org/licenses/mit-license.php (The MIT License) | |
* @see <a href="http://www.ecma-international.org/ecma-262/8.0/#sec-object.assign">19.1.2.1O bject.assign ( target, ...sources ) - ECMAScript® 2017 Language Specification</a> | |
*/ | |
'use strict'; | |
if (typeof Object.assign !== 'function') { | |
Object.defineProperty(Object, 'assign', { | |
writable: true, | |
enumerable: false, | |
configurable: true, | |
value: (function (Object, keys) { | |
return function assign (target, source /* [,... source] */) { | |
'use strict'; | |
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; j < sLen; ++j) { | |
key = sKeys[j]; | |
target[key] = source[key]; | |
} | |
} | |
} | |
return target; | |
}; | |
}(Object, Object.keys)) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment