How to check if object has any properties in JavaScript?

后端 未结 16 2357
自闭症患者
自闭症患者 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:45

    Very late answer, but this is how you could handle it with prototypes.

    Array.prototype.Any = function(func) {
        return this.some(func || function(x) { return x });
    }
    
    Object.prototype.IsAny = function() {
        return Object.keys(this).Any();
    }
    

提交回复
热议问题