-
-
Notifications
You must be signed in to change notification settings - Fork 893
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
Conversation
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]) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Also see #114, which is pretty much the same issue. |
We can’t fix the cases with |
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? |
@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/ |
@joeytwiddle the problem is that IE8 treats |
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?