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. :(
Sebastian Markbage committed
Oct 16, 2014 at 11:49 UTC
25d63b43efdc5a13a174b412233179bb0b79698c
1 file changed
+15
-27
src/vendor/stubs/Object.assign.js
+15
-27
@@ -9,49 +9,37 @@
9
* @providesModule Object.assign
10
*/
11
12
-// This is an optimized version that fails on hasOwnProperty checks
13
-// and non objects. It's not spec-compliant. It's a perf optimization.
14
-
15
-var hasOwnProperty = Object.prototype.hasOwnProperty;
12
+// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.assign
13
14
function assign(target, sources) {
18
- if (__DEV__) {
19
- if (target == null) {
20
- throw new TypeError('Object.assign target cannot be null or undefined');
21
- }
22
- if (typeof target !== 'object' && typeof target !== 'function') {
23
- throw new TypeError(
24
- 'In this environment the target of assign MUST be an object. ' +
25
- 'This error is a performance optimization and not spec compliant.'
26
- );
27
- }
15
+ if (target == null) {
16
+ throw new TypeError('Object.assign target cannot be null or undefined');
17
}
18
19
+ var to = Object(target);
20
+ var hasOwnProperty = Object.prototype.hasOwnProperty;
21
+
22
for (var nextIndex = 1; nextIndex < arguments.length; nextIndex++) {
23
var nextSource = arguments[nextIndex];
24
if (nextSource == null) {
25
continue;
26
}
27
28
+ var from = Object(nextSource);
29
+
30
// We don't currently support accessors nor proxies. Therefore this
31
// copy cannot throw. If we ever supported this then we must handle
38
- // exceptions and side-effects.
39
-
40
- for (var key in nextSource) {
41
- if (__DEV__) {
42
- if (!hasOwnProperty.call(nextSource, key)) {
43
- throw new TypeError(
44
- 'One of the sources to assign has an enumerable key on the ' +
45
- 'prototype chain. This is an edge case that we do not support. ' +
46
- 'This error is a performance optimization and not spec compliant.'
47
- );
48
- }
32
+ // exceptions and side-effects. We don't support symbols so they won't
33
+ // be transferred.
34
+
35
+ for (var key in from) {
36
+ if (hasOwnProperty.call(from, key)) {
37
+ to[key] = from[key];
38
}
50
- target[key] = nextSource[key];
39
}
40
}
41
54
- return target;
42
+ return to;
43
};
44
45
module.exports = assign;