Avoid directly calling hasOwnProperty (#6855)
* Avoid directly calling hasOwnProperty * Fix failing test case
Ali Taheri Moghaddar committed
May 24, 2016 at 22:25 UTC
c313baa0cae1006090274d329de0368f53fb5472
2 files changed
+10
-3
src/isomorphic/classic/element/ReactElement.js
+4
-3
@@ -15,6 +15,7 @@ var ReactCurrentOwner = require('ReactCurrentOwner');
15
16
var warning = require('warning');
17
var canDefineProperty = require('canDefineProperty');
18
+var hasOwnProperty = Object.prototype.hasOwnProperty;
19
20
// The Symbol used to tag the ReactElement type. If there is no native Symbol
21
// nor polyfill, then a plain number is used for performance.
@@ -137,9 +138,9 @@ ReactElement.createElement = function(type, config, children) {
138
'React.createElement(...): Expected props argument to be a plain object. ' +
139
'Properties defined in its prototype chain will be ignored.'
140
);
140
- ref = !config.hasOwnProperty('ref') ||
141
+ ref = !hasOwnProperty.call(config, 'ref') ||
142
Object.getOwnPropertyDescriptor(config, 'ref').get ? null : config.ref;
142
- key = !config.hasOwnProperty('key') ||
143
+ key = !hasOwnProperty.call(config, 'key') ||
144
Object.getOwnPropertyDescriptor(config, 'key').get ? null : '' + config.key;
145
} else {
146
ref = config.ref === undefined ? null : config.ref;
@@ -149,7 +150,7 @@ ReactElement.createElement = function(type, config, children) {
150
source = config.__source === undefined ? null : config.__source;
151
// Remaining properties are added to a new props object
152
for (propName in config) {
152
- if (config.hasOwnProperty(propName) &&
153
+ if (hasOwnProperty.call(config, propName) &&
154
!RESERVED_PROPS.hasOwnProperty(propName)) {
155
props[propName] = config[propName];
156
}
src/isomorphic/classic/element/__tests__/ReactElement-test.js
+6
@@ -138,6 +138,12 @@ describe('ReactElement', function() {
138
expect(element.props.foo).toBe(1);
139
});
140
141
+ it('does not fail if config has no prototype', function() {
142
+ var config = Object.create(null, {foo: {value: 1, enumerable: true}});
143
+ var element = React.createFactory(ComponentClass)(config);
144
+ expect(element.props.foo).toBe(1);
145
+ });
146
+
147
it('warns if the config object inherits from any type other than Object', function() {
148
spyOn(console, 'error');
149
React.createElement('div', {foo: 1});