Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between jQuery.isPlainObject() and jQuery.isEmptyObject()

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.

like image 458
maximus Avatar asked Apr 24 '11 22:04

maximus


2 Answers

$.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.

like image 88
BoltClock Avatar answered Nov 15 '22 07:11

BoltClock


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
like image 32
typeof Avatar answered Nov 15 '22 09:11

typeof