jashkenas / jashkenas/underscore

Inconsistent results when passing empty jQuery object to _.isEmpty

Open
#1,715 13 comments 0 reactions 0 assignees View on GitHub
breaking change question
Dominant language
JavaScript
Stars
27.3k
Forks
5.4k
Avg merge
2d 5h
Merged PRs (30d)
1

Description

I am noticing some strange behavior when running _.isEmpty on an empty jQuery object. At first, _.isEmpty returns false, but if I add an empty collection to it the collection, it stays empty, but _.isEmpty returns true after that. Here is an example:

```
var result = $();
console.log("before", _(result).isEmpty(), result.length); // before true 0
result = result.add($("p"));
console.log("after", _(result).isEmpty(), result.length); // after false 0
```

This is on a page with no paragraphs, so the result should stay empty after adding `$("p")`. Is this a bug in Underscore, or is it in jQuery, or am I using isEmpty wrong? It seems like adding the empty collection creates some new properties in the result (length, prevObject, context), but it seems confusing that isEmpty sometimes returns true on empty array-like objects, and sometimes returns false.

I would try to diagnose the problem myself but I am busy. Here is a fiddle: http://jsfiddle.net/56kaw/

Contributor guide

Open the contributing guide

Research direction

Start with Underscore's _.isEmpty behavior and reproduce the before/after example from the issue using an empty jQuery object and result.add($('p')). Inspect the resulting length, prevObject, and context properties, then determine whether the inconsistency is in Underscore or jQuery. Done means a confirmed cause and a regression test or clear resolution.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, jquery
Domain
web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.