首页 > 代码库 > 判断Javascript对象是否为空

判断Javascript对象是否为空

判断普通javascript对象是否为空(含有可枚举的属性,自有的、继承的都可以),可使用jQuery 3.2.1版的isEmptyObject()方法:

isEmptyObject: function( obj ) {
    var name;
    for ( name in obj ) {
        return false;
    }
    return true;
}

As of jQuery 1.4 this method checks both properties on the object itself and properties inherited from prototypes (in that it doesn‘t use hasOwnProperty). The argument should always be a plain JavaScript Object as other types of object (DOM elements, primitive strings/numbers, host objects) may not give consistent results across browsers. To determine if an object is a plain JavaScript object, use $.isPlainObject()

判断Javascript对象是否为空