Skip to main content

Bug Tracker

Side navigation

#9989 closed enhancement (duplicate)

Opened August 05, 2011 02:47PM UTC

Closed August 05, 2011 03:16PM UTC

Last modified March 14, 2012 08:22AM UTC

$.each doesn't check for null

Reported by: mjlyco2@gmail.com Owned by: rwaldron
Priority: low Milestone:
Component: core Version: 1.6.2
Keywords: Cc:
Blocked by: Blocking:
Description
var something = ['one', 'two', 'three'];

// some more code

// then woops!
something = null;

$.each(something, function(){
  console.log(this);
});

The code above will cause a js error on something.length.

to fix it you need do do the following... maybe this check should be incorporated?

var something = ['one', 'two', 'three'];

// some more code

// then woops!
something = null;

if (something != nul) {
  $.each(something, function(){
    console.log(this);
  });
}
Attachments (0)
Change History (7)

Changed August 05, 2011 03:11PM UTC by rwaldron comment:1

component: unfiledcore
milestone: None1.6.3
owner: → rwaldron
priority: undecidedlow
status: newassigned

Changed August 05, 2011 03:14PM UTC by rwaldron comment:2

resolution: → wontfix
status: assignedclosed

Thanks for reporting this, however jQuery cannot account for every possible arbitrary programming structure mistake. I recommend making a check for null before using $.each

Changed August 05, 2011 03:15PM UTC by rwaldron comment:3

resolution: wontfix
status: closedreopened

Changed August 05, 2011 03:16PM UTC by rwaldron comment:4

resolution: → duplicate
status: reopenedclosed

Changed August 05, 2011 03:16PM UTC by rwaldron comment:5

Duplicate of #8547.

Changed August 05, 2011 03:17PM UTC by dmethvin comment:6

You can also say $.each(something || [], ...) and either indicates to other readers and your later self that you *knew* something could be null. This not only avoids the error, but provides valuable documentation about the program state.

Changed August 26, 2011 01:33AM UTC by dmethvin comment:7

milestone: 1.6.3