Skip to content

Commit 00575d4

Browse files
committed
Core: restore enumeration behavior in isPlainObject
Fixes gh-2968 Close gh-2970
1 parent bb235ed commit 00575d4

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/core.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ jQuery.extend( {
225225
},
226226

227227
isPlainObject: function( obj ) {
228+
var key;
228229

229230
// Not plain objects:
230231
// - Any object or value whose internal [[Class]] property is not "[object Object]"
@@ -239,9 +240,11 @@ jQuery.extend( {
239240
return false;
240241
}
241242

242-
// If the function hasn't returned already, we're confident that
243-
// |obj| is a plain object, created by {} or constructed with new Object
244-
return true;
243+
// Own properties are enumerated firstly, so to speed up,
244+
// if last one is own, then all properties are own
245+
for ( key in obj ) {}
246+
247+
return key === undefined || hasOwn.call( obj, key );
245248
},
246249

247250
isEmptyObject: function( obj ) {

test/unit/core.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,21 @@ QUnit.test( "type for `Symbol`", function( assert ) {
286286
});
287287

288288
QUnit.asyncTest( "isPlainObject", function( assert ) {
289-
assert.expect( 15 );
289+
assert.expect( 19 );
290290

291-
var pass, iframe, doc,
291+
var pass, iframe, doc, parentObj, childObj, deep,
292292
fn = function() {};
293293

294294
// The use case that we want to match
295295
assert.ok( jQuery.isPlainObject( {} ), "{}" );
296+
assert.ok( jQuery.isPlainObject( new window.Object() ), "new Object" );
297+
298+
parentObj = { foo: "bar" };
299+
childObj = Object.create( parentObj );
300+
301+
assert.ok( !jQuery.isPlainObject( childObj ), "isPlainObject(Object.create({}))" );
302+
childObj.bar = "foo";
303+
assert.ok( !jQuery.isPlainObject( childObj ), "isPlainObject(Object.create({}))" );
296304

297305
// Not objects shouldn't be matched
298306
assert.ok( !jQuery.isPlainObject( "" ), "string" );
@@ -320,6 +328,10 @@ QUnit.asyncTest( "isPlainObject", function( assert ) {
320328
// Again, instantiated objects shouldn't be matched
321329
assert.ok( !jQuery.isPlainObject( new fn() ), "new fn" );
322330

331+
// Deep object
332+
deep = { "foo": { "baz": true }, "foo2": document };
333+
assert.ok( jQuery.isPlainObject( deep ), "Object with objects is still plain" );
334+
323335
// DOM Element
324336
assert.ok( !jQuery.isPlainObject( document.createElement( "div" ) ), "DOM Element" );
325337

0 commit comments

Comments
 (0)