define( [
"./var/arr",
"./var/document",
+ "./var/getProto",
"./var/slice",
"./var/concat",
"./var/push",
"./var/class2type",
"./var/toString",
"./var/hasOwn",
+ "./var/fnToString",
+ "./var/ObjectFunctionString",
"./var/support",
"./core/DOMEval"
-], function( arr, document, slice, concat,
- push, indexOf, class2type, toString, hasOwn, support, DOMEval ) {
+], function( arr, document, getProto, slice, concat, push, indexOf,
+ class2type, toString, hasOwn, fnToString, ObjectFunctionString,
+ support, DOMEval ) {
var
version = "@VERSION",
},
isPlainObject: function( obj ) {
- var key;
+ var proto, Ctor;
- // Not plain objects:
- // - Any object or value whose internal [[Class]] property is not "[object Object]"
- // - DOM nodes
- // - window
- if ( jQuery.type( obj ) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
+ // Detect obvious negatives
+ // Use toString instead of jQuery.type to catch host objects
+ if ( !obj || toString.call( obj ) !== "[object Object]" ) {
return false;
}
- // Not own constructor property must be Object
- if ( obj.constructor &&
- !hasOwn.call( obj, "constructor" ) &&
- !hasOwn.call( obj.constructor.prototype || {}, "isPrototypeOf" ) ) {
- return false;
- }
+ proto = getProto( obj );
- // Own properties are enumerated firstly, so to speed up,
- // if last one is own, then all properties are own
- for ( key in obj ) {}
+ // Objects with no prototype (e.g., `Object.create( null )`) are plain
+ if ( !proto ) {
+ return true;
+ }
- return key === undefined || hasOwn.call( obj, key );
+ // Objects with prototype are plain iff they were constructed by a global Object function
+ Ctor = hasOwn.call( proto, "constructor" ) && proto.constructor;
+ return typeof Ctor === "function" && fnToString.call( Ctor ) === ObjectFunctionString;
},
isEmptyObject: function( obj ) {
QUnit.asyncTest( "isPlainObject", function( assert ) {
- assert.expect( 22 );
+ assert.expect( 23 );
var pass, iframe, doc, parentObj, childObj, deep,
fn = function() {};
assert.ok( jQuery.isPlainObject( { constructor: "foo" } ),
"plain object with primitive constructor property" );
- parentObj = { foo: "bar" };
+ parentObj = {};
childObj = Object.create( parentObj );
-
- assert.ok( !jQuery.isPlainObject( childObj ), "isPlainObject(Object.create({}))" );
+ assert.ok( !jQuery.isPlainObject( childObj ), "Object.create({})" );
+ parentObj.foo = "bar";
+ assert.ok( !jQuery.isPlainObject( childObj ), "Object.create({...})" );
childObj.bar = "foo";
- assert.ok( !jQuery.isPlainObject( childObj ), "isPlainObject(Object.create({}))" );
+ assert.ok( !jQuery.isPlainObject( childObj ), "extend(Object.create({...}), ...)" );
// Not objects shouldn't be matched
assert.ok( !jQuery.isPlainObject( "" ), "string" );