Skip to content

Commit 63397aa

Browse files
committed
Core: restore enumeration behavior in isPlainObject
Fixes jquerygh-2968 Close jquerygh-2970
1 parent c5c3073 commit 63397aa

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
@@ -223,6 +223,7 @@ jQuery.extend( {
223223
},
224224

225225
isPlainObject: function( obj ) {
226+
var key;
226227

227228
// Not plain objects:
228229
// - Any object or value whose internal [[Class]] property is not "[object Object]"
@@ -237,9 +238,11 @@ jQuery.extend( {
237238
return false;
238239
}
239240

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

245248
isEmptyObject: function( obj ) {

test/unit/core.js

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

270270
QUnit.asyncTest( "isPlainObject", function( assert ) {
271-
assert.expect( 15 );
271+
assert.expect( 19 );
272272

273-
var pass, iframe, doc,
273+
var pass, iframe, doc, parentObj, childObj, deep,
274274
fn = function() {};
275275

276276
// The use case that we want to match
277277
assert.ok( jQuery.isPlainObject( {} ), "{}" );
278+
assert.ok( jQuery.isPlainObject( new window.Object() ), "new Object" );
279+
280+
parentObj = { foo: "bar" };
281+
childObj = Object.create( parentObj );
282+
283+
assert.ok( !jQuery.isPlainObject( childObj ), "isPlainObject(Object.create({}))" );
284+
childObj.bar = "foo";
285+
assert.ok( !jQuery.isPlainObject( childObj ), "isPlainObject(Object.create({}))" );
278286

279287
// Not objects shouldn't be matched
280288
assert.ok( !jQuery.isPlainObject( "" ), "string" );
@@ -302,6 +310,10 @@ QUnit.asyncTest( "isPlainObject", function( assert ) {
302310
// Again, instantiated objects shouldn't be matched
303311
assert.ok( !jQuery.isPlainObject( new fn() ), "new fn" );
304312

313+
// Deep object
314+
deep = { "foo": { "baz": true }, "foo2": document };
315+
assert.ok( jQuery.isPlainObject( deep ), "Object with objects is still plain" );
316+
305317
// DOM Element
306318
assert.ok( !jQuery.isPlainObject( document.createElement( "div" ) ), "DOM Element" );
307319

0 commit comments

Comments
 (0)