Can anyone explain what's the difference between jQuery.isPlainObject() and jQuery.isEmptyObject()? They both return true for an object that has no properties. Examples
jQuery.isEmptyObject({}); // returns true
jQuery.isPlainObject({}); // returns true
Thanks in advance.
Can anyone explain what's the difference between jQuery.isPlainObject() and jQuery.isEmptyObject()? They both return true for an object that has no properties. Examples
jQuery.isEmptyObject({}); // returns true
jQuery.isPlainObject({}); // returns true
Thanks in advance.
Share Improve this question edited Apr 24, 2011 at 22:52 BoltClock 724k165 gold badges1.4k silver badges1.4k bronze badges asked Apr 24, 2011 at 22:40 maximusmaximus 2,4375 gold badges40 silver badges56 bronze badges2 Answers
Reset to default 14$.isEmptyObject()
doesn't consider the object's type, or how it was created; as long as it has totally no properties this function returns true.
$.isPlainObject()
returns true for objects which are pure Object
instances; false for objects that are of any other type, e.g. Number
, String
, Function
or a custom type.
From the manual for $.isPlainObject()
:
Description: Check to see if an object is a plain object (created using "{}" or "new Object").
So checking an empty object literal {}
with this function would return true, because that's an instance of the plain Object
class. And since it's empty, $.isEmptyObject()
also returns true.
jQuery.isEmptyObject()
This function will return true if the object is empty (as the name suggests).
jQuery.isPlainObject()
This function will return true if it is an object literal or (less commonly) the object is created with "new Object()
".
This example may help:
jQuery.isEmptyObject({ 'try' : 'this' }); // returns false
jQuery.isPlainObject({ 'try' : 'this' }); // returns true