Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

indexOf doesn't work with undefined in ie8 #131

Closed
wants to merge 1 commit into from

Conversation

kybernetikos
Copy link

The check in line 511 breaks the behaviour of indexOf in ie8.

Test case: [undefined].indexOf(undefined)
Expected: return 0
Actual: return -1

The reason for this is because in ie8

0 in [undefined]

returns false, whereas in more modern browsers it returns true.

I'm not sure why this check was being done in the first place and think it may not be required (hence my proposed change). If it is required, can we find a way to achieve the same thing without breaking ie8?

This check breaks ie8.

Test case: [undefined].indexOf(undefined) returns -1 rather than 0

The reason for this is because in ie8

0 in [undefined] returns false, whereas in more modern browsers it returns true.

I'm not sure why this check was being done in the first place and think it may not be required (hence my proposed change).  If it is required, can we find a way to achieve the same thing without breaking ie8?
@@ -508,7 +508,7 @@ if (!Array.prototype.lastIndexOf || ([0, 1].lastIndexOf(0, -3) != -1)) {
// handle negative indices
i = i >= 0 ? i : length - Math.abs(i);
for (; i >= 0; i--) {
if (i in self && sought === self[i]) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check breaks ie8 for the test case

[undefined].indexOf(undefined)

because in ie8 (but not internet explorer 9 and above)

0 in [undefined]

returns false.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per-specs, "indexOf" should work on sparse arrays, so that means:

[,,,].indexOf(undefined)

Should return -1. The in check is there to verify the existence of a numeric property at the current index. Otherwise, reading from a non-existing property in the object would yield undefined and this function would in turn return 0, which is incorrect.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[undefined, undefined, undefined, undefined].indexOf(undefined) returns 0 in firefox and chrome. That's because it's not a sparse array but an array with undefined in it. You can tell the difference because your example does return -1.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to explain why the in check was there in the first place. Indeed an array filled with undefined values is not sparse, as per specs, but it appears that IE8- treats undefined as an elision, creating a sparse array. It's an IE8- bug.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I worked out what you meant after I commented. It's a bit irritating for us because it causes a bug, but I can see now that to fix it would cause bugs for other people.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a tough choice.

@robotlolita
Copy link
Contributor

Also see #114, which is pretty much the same issue.

@kriskowal
Copy link
Member

We can’t fix the cases with undefined and sparse arrays in IE <= 8 with a shim. I’m inclined to inclined to not test for it and to make a note of the caveat in readme.

@joeytwiddle
Copy link

At the risk of enraging the duck-typing/feature-polling gods, couldn't you work around this by testing explicitly for older versions of IE, and doing a different fix for them than you do for other browsers?

@ljharb
Copy link
Member

ljharb commented Jan 11, 2014

@joeytwiddle Unfortunately, because it's a bug in the JS engine itself in IE 6-8, it's impossible to work around. If you can find a way to distinguish between an undefined value and an absent key in an array in those browsers, I'd love to include it. See http://jsfiddle.net/wrq6t/1/

@robotlolita
Copy link
Contributor

@joeytwiddle the problem is that IE8 treats [undefined, undefined] the same way as [,,]. So even if you knew that you were dealing with an IE8 engine, afaik, there's no way to distinguish between an actual elision and IE8's bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants