Skip to content

Instantly share code, notes, and snippets.

@mavame mavame/assign.js
Last active Aug 4, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.