Use hasOwnProperty checks in Object.assign

Because IE8 polyfills add enumerable properties, this doesn't play nicely.
So for IE8 compatibility we add this check. Slower though. :(
This commit is contained in:
Sebastian Markbage
2014-10-16 11:49:50 -07:00
parent 828efb798a
commit 25d63b43ef
+14 -26
View File
@@ -9,49 +9,37 @@
* @providesModule Object.assign
*/
// This is an optimized version that fails on hasOwnProperty checks
// and non objects. It's not spec-compliant. It's a perf optimization.
var hasOwnProperty = Object.prototype.hasOwnProperty;
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.assign
function assign(target, sources) {
if (__DEV__) {
if (target == null) {
throw new TypeError('Object.assign target cannot be null or undefined');
}
if (typeof target !== 'object' && typeof target !== 'function') {
throw new TypeError(
'In this environment the target of assign MUST be an object. ' +
'This error is a performance optimization and not spec compliant.'
);
}
if (target == null) {
throw new TypeError('Object.assign target cannot be null or undefined');
}
var to = Object(target);
var hasOwnProperty = Object.prototype.hasOwnProperty;
for (var nextIndex = 1; nextIndex < arguments.length; nextIndex++) {
var nextSource = arguments[nextIndex];
if (nextSource == null) {
continue;
}
var from = Object(nextSource);
// We don't currently support accessors nor proxies. Therefore this
// copy cannot throw. If we ever supported this then we must handle
// exceptions and side-effects.
// exceptions and side-effects. We don't support symbols so they won't
// be transferred.
for (var key in nextSource) {
if (__DEV__) {
if (!hasOwnProperty.call(nextSource, key)) {
throw new TypeError(
'One of the sources to assign has an enumerable key on the ' +
'prototype chain. This is an edge case that we do not support. ' +
'This error is a performance optimization and not spec compliant.'
);
}
for (var key in from) {
if (hasOwnProperty.call(from, key)) {
to[key] = from[key];
}
target[key] = nextSource[key];
}
}
return target;
return to;
};
module.exports = assign;