jQuery.isPlainObject()和jQuery.isEmptyObject()之间的区别

max*_*mus 5 javascript jquery object

任何人都可以解释jQuery.isPlainObject()和jQuery.isEmptyObject()之间的区别是什么?对于没有属性的对象,它们都返回true.例子

jQuery.isEmptyObject({}); // returns true
jQuery.isPlainObject({}); // returns true
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Bol*_*ock 13

$.isEmptyObject()不考虑对象的类型或它是如何创建的; 只要它完全没有属性,该函数返回true.

$.isPlainObject()对于纯Object实例的对象返回true ; 假为任何其他类型的对象,例如Number,String,Function或自定义类型.


手动$.isPlainObject():

描述:检查对象是否是普通对象(使用"{}"或"new Object"创建).

因此,{}使用此函数检查空对象文字将返回true,因为这是普通Object类的实例.因为它是空的,$.isEmptyObject()也会返回true.


typ*_*eof 5

jQuery.isEmptyObject()
Run Code Online (Sandbox Code Playgroud)

如果对象为空,则此函数将返回true(顾名思义).

jQuery.isPlainObject()
Run Code Online (Sandbox Code Playgroud)

如果它是对象文字或(不太常见)使用" new Object()" 创建对象,则此函数将返回true .

此示例可能有所帮助:

jQuery.isEmptyObject({ 'try' : 'this' }); // returns false
jQuery.isPlainObject({ 'try' : 'this' }); // returns true
Run Code Online (Sandbox Code Playgroud)