Posts Tagged: IE8

Поведение `for (var i in arr)` в IE8 после расширения Array.prototype

Недавно решил посмотреть, как дела у моего jQuery плагина в IE8. Я думаю, предсказать что было дальше не сложнее, чем предугадать, чем закончится классическая голливудская мелодрама – в скрипте обнаружились ошибки.

Из-за убогости консоли я долго думал в чём дело и столкнулся с необычным явлением. Как известно, IE8, как и все другие десктопные браузеры от Microsoft с момента своего появления является сильно устаревшим браузером. В частности, в нём нет ряда методов из Array.prototype, таких как весьма удобный и относительно часто используемый indexOf(obj). Естественно я знал это и позаботился, добавив к проекту небольшой скрипт missed.js, который кочует у меня из проекта в проект, разрешая проблему недостающего функционала в IE8. Этот скрипт – классическое решение, он содержит ряд объявлений вида:

if (!Array.prototype.indexOf) { 
    Array.prototype.indexOf = function(...) {...} 
}

Не знаю, почему я впервые наткнулся на ошибку только недавно, но в общем это был первый раз, когда я увидел, как после этого будет использоваться код вида for (var i in arr), в случае, когда arr – массив.

Оказывается (с одной стороны конечно же логично, но с другой это далеко не сразу приходит в голову), что в таком случае i принимает значение не только индексов элементов массива, но и забирает не родные методы из своего, т.е. Array прототайпа. Поэтому использование такой конструкции без дополнительных правок (например делать continue, если i не является числом) невозможно.

Обсудив это с коллегой я пришёл к выводу, что лучше для массивов всегда использовать код вида for (var i = 0; i < arr.length; i++).

`for (var i in arr)` behavior in IE8 after extending Array.prototype

Few days ago I decided to see how is my jQuery plugin doing in IE8. I think to predict what happened next is not much more difficult than to guess how a classical Hollywood melodrama ends. There were errors in the script.

Due to the IE console is incredibly poor I spent a lot of time considering what was going and faced an interesting phenomenon. As you know, IE8 like all other desktop browsers from Microsoft corporation is heavily outdated since the time it was released. For instance, there is luck of some methods from Array.prototype, such as very handy and quite popular indexOf(obj). I knew about that of course and that is why I added my missed.js script I use in many projects for a long time, which is a collection of declarations like:

if (!Array.prototype.indexOf) { 
    Array.prototype.indexOf = function(...) {...} 
}

This is strange it was the first time I faced the error only now, but anyhow it was the first time I saw how for (var i in arr) construction will work after extending Array.prototype if
arr is an array.

It turned out (what is clear on the one hand, but is not so predictable on the other) that in this case, i takes not only indices valus, but also not native methods from its (Array) prototype. That is why using such construction is not impossible without additional editings like doint continue if i is not a mumber.

After talking to my college about that I made a conclusion that if you dealing with arrays it is always better to use for (var i = 0; i < arr.length; i++) code.