diff --git a/test/objects.js b/test/objects.js index 8743eaed8..272b66ef1 100644 --- a/test/objects.js +++ b/test/objects.js @@ -60,6 +60,8 @@ $(document).ready(function() { ok(_.isEmpty([]), '[] is empty'); ok(!_.isEmpty({one : 1}), '{one : 1} is not empty'); ok(_.isEmpty({}), '{} is empty'); + ok(_.isEmpty(null), 'null is empty'); + ok(_.isEmpty(), 'undefined is empty'); var obj = {one : 1}; delete obj.one; diff --git a/underscore.js b/underscore.js index 538019fc8..88433489f 100644 --- a/underscore.js +++ b/underscore.js @@ -503,7 +503,9 @@ // Is a given array or object empty? _.isEmpty = function(obj) { - return _.keys(obj).length == 0; + if (_.isArray(obj)) return obj.length===0; + for (var k in obj) return false; + return true; }; // Is a given value a DOM element?