Bug Tracker

Ticket #9989 (closed enhancement: duplicate)

Opened 3 years ago

Last modified 3 years ago

$.each doesn't check for null

Reported by: mjlyco2@… Owned by: rwaldron
Priority: low Milestone:
Component: core Version: 1.6.2
Keywords: Cc:
Blocking: Blocked by:

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

comment:1 Changed 3 years ago by rwaldron

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

comment:2 Changed 3 years ago by rwaldron

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

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

  • Status changed from closed to reopened
  • Resolution wontfix deleted

comment:4 Changed 3 years ago by rwaldron

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

comment:5 Changed 3 years ago by rwaldron

Duplicate of #8547.

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

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