Equivalent of jQuery's isEmptyObject()

279 views
Skip to first unread message

Blackbird

unread,
Nov 11, 2012, 6:01:41 PM11/11/12
to mootool...@googlegroups.com
Hi,

Does Mootools have an equivalent to jQuery's isEmptyObject() function?
http://api.jquery.com/jQuery.isEmptyObject

Arian Stolwijk

unread,
Nov 11, 2012, 6:09:46 PM11/11/12
to mootool...@googlegroups.com
You could use:

Object.every({}, function(){ return false }) // true
Object.every({a: 1}, function(){ return false }) // false

Arian Stolwijk

unread,
Nov 11, 2012, 6:12:21 PM11/11/12
to mootool...@googlegroups.com
Or with simple JS:

var empty = true;
for (var k in object){ empty = false; break; }

Blackbird

unread,
Nov 11, 2012, 6:26:57 PM11/11/12
to mootool...@googlegroups.com
Thanks for the reply. I think I will go for either of those JS solutions, found on Stackoverflow:

function isEmpty(obj) {
    for(var prop in obj) {
        if(obj.hasOwnProperty(prop))
            return false;
    }

    return true;
}

Or for ECMAScript 5 compatible browsers: Object.keys( obj ).length === 0


I think jQuery's function is useful, and would be welcome in Mootools.

Sanford Whiteman

unread,
Nov 11, 2012, 9:27:36 PM11/11/12
to Blackbird
> Thanks for the reply. I think I will go for either of those JS solutions,
> found on Stackoverflow:

> function isEmpty(obj) {
> for(var prop in obj) {
> if(obj.hasOwnProperty(prop))
> return false;
> }

> return true;}

A solution using hasOwnProp, while it may seem most complete, does
make its own assumption about emptiness. This may make it compatible
with jQuery but it's not the only definition: deliberately inherited
properties may suffice to make an object non-empty depending on your
ecosystem. YMMV....

-- Sandy

Reply all
Reply to author
Forward
0 new messages