From f7c9d93b537ae62a5f62fd7262a7e5bc27b809af Mon Sep 17 00:00:00 2001 From: Ali Taheri Moghaddar Date: Tue, 24 May 2016 22:25:51 +0430 Subject: [PATCH] Avoid directly calling hasOwnProperty (#6855) * Avoid directly calling hasOwnProperty * Fix failing test case (cherry picked from commit c313baa0cae1006090274d329de0368f53fb5472) --- src/isomorphic/classic/element/ReactElement.js | 7 ++++--- .../classic/element/__tests__/ReactElement-test.js | 6 ++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/isomorphic/classic/element/ReactElement.js b/src/isomorphic/classic/element/ReactElement.js index 40c1c7fc29d53..b0a6648870de7 100644 --- a/src/isomorphic/classic/element/ReactElement.js +++ b/src/isomorphic/classic/element/ReactElement.js @@ -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. @@ -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; @@ -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]; } diff --git a/src/isomorphic/classic/element/__tests__/ReactElement-test.js b/src/isomorphic/classic/element/__tests__/ReactElement-test.js index fac4c6c416f2f..011dbf9aa8b68 100644 --- a/src/isomorphic/classic/element/__tests__/ReactElement-test.js +++ b/src/isomorphic/classic/element/__tests__/ReactElement-test.js @@ -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});