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.
$.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With