Skip to content

Commit

Permalink
Avoid directly calling hasOwnProperty (facebook#6855)
Browse files Browse the repository at this point in the history
* Avoid directly calling hasOwnProperty

* Fix failing test case

(cherry picked from commit c313baa)
  • Loading branch information
alitaheri authored and zpao committed Jun 8, 2016
1 parent 32f9655 commit f7c9d93
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/isomorphic/classic/element/ReactElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var ReactCurrentOwner = require('ReactCurrentOwner');

var warning = require('warning');
var canDefineProperty = require('canDefineProperty');
var hasOwnProperty = Object.prototype.hasOwnProperty;

// The Symbol used to tag the ReactElement type. If there is no native Symbol
// nor polyfill, then a plain number is used for performance.
Expand Down Expand Up @@ -137,9 +138,9 @@ ReactElement.createElement = function(type, config, children) {
'React.createElement(...): Expected props argument to be a plain object. ' +
'Properties defined in its prototype chain will be ignored.'
);
ref = !config.hasOwnProperty('ref') ||
ref = !hasOwnProperty.call(config, 'ref') ||
Object.getOwnPropertyDescriptor(config, 'ref').get ? null : config.ref;
key = !config.hasOwnProperty('key') ||
key = !hasOwnProperty.call(config, 'key') ||
Object.getOwnPropertyDescriptor(config, 'key').get ? null : '' + config.key;
} else {
ref = config.ref === undefined ? null : config.ref;
Expand All @@ -149,7 +150,7 @@ ReactElement.createElement = function(type, config, children) {
source = config.__source === undefined ? null : config.__source;
// Remaining properties are added to a new props object
for (propName in config) {
if (config.hasOwnProperty(propName) &&
if (hasOwnProperty.call(config, propName) &&
!RESERVED_PROPS.hasOwnProperty(propName)) {
props[propName] = config[propName];
}
Expand Down
6 changes: 6 additions & 0 deletions src/isomorphic/classic/element/__tests__/ReactElement-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ describe('ReactElement', function() {
expect(element.props.foo).toBe(1);
});

it('does not fail if config has no prototype', function() {
var config = Object.create(null, {foo: {value: 1, enumerable: true}});
var element = React.createFactory(ComponentClass)(config);
expect(element.props.foo).toBe(1);
});

it('warns if the config object inherits from any type other than Object', function() {
spyOn(console, 'error');
React.createElement('div', {foo: 1});
Expand Down

0 comments on commit f7c9d93

Please sign in to comment.