Skip to content

Instantly share code, notes, and snippets.

@mavame
Last active August 4, 2017 18:26
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 mavame/c8625087d1793179f6caca5a05f8f9f5 to your computer and use it in GitHub Desktop.
Save mavame/c8625087d1793179f6caca5a05f8f9f5 to your computer and use it in GitHub Desktop.
Assign function instead of MDN polyfill
/**
* 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