Last active
August 4, 2017 18:26
-
-
Save mavame/c8625087d1793179f6caca5a05f8f9f5 to your computer and use it in GitHub Desktop.
Assign function instead of MDN polyfill
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
/** | |
* Copy the values of all enumerable own properties from one or more objects to a target object. | |
* Portion taken from the Object.assign(target, ...sources) polyfill on MDN. | |
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill | |
* @param {object} target Target object | |
* @param {...object} sources Additional objects whose properties to copy into target | |
* @return {object} target | |
*/ | |
export default function assign(target, ...sources) { | |
if (target == null) { // TypeError if undefined or null | |
throw new TypeError('Cannot convert undefined or null to object'); | |
} | |
var to = target; | |
for (var index = 1; index < arguments.length; index++) { | |
var nextSource = arguments[index]; | |
if (nextSource != null) { // Skip over if undefined or null | |
for (var nextKey in nextSource) { | |
// Avoid bugs when hasOwnProperty is shadowed | |
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) { | |
to[nextKey] = nextSource[nextKey]; | |
} | |
} | |
} | |
} | |
return to; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment