最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Difference between jQuery.isPlainObject() and jQuery.isEmptyObject() - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a comment  | 

2 Answers 2

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
发布评论

评论列表(0)

  1. 暂无评论