#1023 ✓invalid

Enumerable#include non-standard behavior

Reported by blatyo | April 6th, 2010 @ 02:23 PM

  function include(object) {
    if (Object.isFunction(this.indexOf))
      if (this.indexOf(object) != -1) return true;

    var found = false;
    this.each(function(value) {
      if (value == object) {
        found = true;
        throw $break;
    return found;

Enumberable#include defaults to the native browser implementation indexOF if it exists for the object passed in. If it does not exist for the object passed in an iterator is used to perform the same function. The documentation for this function says that this method uses "equality with implicit type conversion". However, on chrome (possibly others) indexOf does not do equality with implicit conversion. For example:

  [1].indexOf('1')   //=> -1
  [0].indexOf(false) //=> -1

Comments and changes to this ticket

  • Juriy Zaytsev

    Juriy Zaytsev April 6th, 2010 @ 02:42 PM

    Chrome must be following behavior of ES5 Array.prototype.indexOf, which is to use so-called "strict equality comparison algorithm" (see 11.9.6); that's the same algorithm used by === operator, or Array.prototype.lastIndexOf.

  • Tobie Langel

    Tobie Langel April 6th, 2010 @ 08:05 PM

    • State changed from “new” to “invalid”

    @blatyo: Please give the Enum#include function a second read. You're description of it is erroneous (though I agree that the code is difficult to read).

    The use of Array#indexOf is for perf only and the call falls back on using the regular loop when Array#indexOf does not return a positive result. So basically, the method behaves exactly as documented in all supported browsers (whether Array#indexOfis implemented or not).

    @kangax: afaik, all browsers implementing Array#indexOf follow the spec. I'm not sure what this has to do with our issue, though.

    Closing this as invalid.

  • blatyo

    blatyo April 6th, 2010 @ 08:47 PM

    @Tobie, I see what you're saying. My question now is why does it work that way? Why not simply restrict it to === ?

  • Tobie Langel

    Tobie Langel April 7th, 2010 @ 01:29 AM

    Two words: backwards compatibility :-/

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins

People watching this ticket