},
isPlainObject: function( obj ) {
+ var key;
// Not plain objects:
// - Any object or value whose internal [[Class]] property is not "[object Object]"
return false;
}
- // If the function hasn't returned already, we're confident that
- // |obj| is a plain object, created by {} or constructed with new Object
- return true;
+ // Own properties are enumerated firstly, so to speed up,
+ // if last one is own, then all properties are own
+ for ( key in obj ) {}
+
+ return key === undefined || hasOwn.call( obj, key );
},
isEmptyObject: function( obj ) {
} );
QUnit.asyncTest( "isPlainObject", function( assert ) {
- assert.expect( 15 );
+ assert.expect( 19 );
- var pass, iframe, doc,
+ var pass, iframe, doc, parentObj, childObj, deep,
fn = function() {};
// The use case that we want to match
assert.ok( jQuery.isPlainObject( {} ), "{}" );
+ assert.ok( jQuery.isPlainObject( new window.Object() ), "new Object" );
+
+ parentObj = { foo: "bar" };
+ childObj = Object.create( parentObj );
+
+ assert.ok( !jQuery.isPlainObject( childObj ), "isPlainObject(Object.create({}))" );
+ childObj.bar = "foo";
+ assert.ok( !jQuery.isPlainObject( childObj ), "isPlainObject(Object.create({}))" );
// Not objects shouldn't be matched
assert.ok( !jQuery.isPlainObject( "" ), "string" );
// Again, instantiated objects shouldn't be matched
assert.ok( !jQuery.isPlainObject( new fn() ), "new fn" );
+ // Deep object
+ deep = { "foo": { "baz": true }, "foo2": document };
+ assert.ok( jQuery.isPlainObject( deep ), "Object with objects is still plain" );
+
// DOM Element
assert.ok( !jQuery.isPlainObject( document.createElement( "div" ) ), "DOM Element" );