Bug Tracker

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#9989 closed enhancement (duplicate)

$.each doesn't check for null

Reported by: mjlyco2@… Owned by: Rick Waldron
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);
  });
}

Change History (7)

comment:1 Changed 6 years ago by Rick Waldron

Component: unfiledcore
Milestone: None1.6.3
Owner: set to Rick Waldron
Priority: undecidedlow
Status: newassigned

comment:2 Changed 6 years ago by Rick Waldron

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

comment:3 Changed 6 years ago by Rick Waldron

Resolution: wontfix
Status: closedreopened

comment:4 Changed 6 years ago by Rick Waldron

Resolution: duplicate
Status: reopenedclosed

comment:5 Changed 6 years ago by Rick Waldron

Duplicate of #8547.

comment:6 Changed 6 years ago by dmethvin

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.

comment:7 Changed 6 years ago by dmethvin

Milestone: 1.6.3
Note: See TracTickets for help on using tickets.