How to check if object has any properties in JavaScript?

后端 未结 16 2323
自闭症患者
自闭症患者 2020-12-04 08:10

Assuming I declare

var ad = {}; 

How can I check whether this object will contain any user-defined properties?

16条回答
  •  一向
    一向 (楼主)
    2020-12-04 08:46

    When sure that the object is a user-defined one, the easiest way to determine if UDO is empty, would be the following code:

    isEmpty=
    /*b.b Troy III p.a.e*/
    function(x,p){for(p in x)return!1;return!0};
    

    Even though this method is (by nature) a deductive one, - it's the quickest, and fastest possible.

    a={};
    isEmpty(a) >> true
    
    a.b=1
    isEmpty(a) >> false 
    

    p.s.: !don't use it on browser-defined objects.

提交回复
热议问题