Bug Tracker

Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#9989 closed enhancement (duplicate)

$.each doesn't check for null

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

Change History (7)

comment:1 Changed 5 years ago by rwaldron

  • Component changed from unfiled to core
  • Milestone changed from None to 1.6.3
  • Owner set to rwaldron
  • Priority changed from undecided to low
  • Status changed from new to assigned

comment:2 Changed 5 years ago by rwaldron

  • Resolution set to wontfix
  • Status changed from assigned to closed

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 5 years ago by rwaldron

  • Resolution wontfix deleted
  • Status changed from closed to reopened

comment:4 Changed 5 years ago by rwaldron

  • Resolution set to duplicate
  • Status changed from reopened to closed

comment:5 Changed 5 years ago by rwaldron

Duplicate of #8547.

comment:6 Changed 5 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 5 years ago by dmethvin

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