This is not how arrays work in JavaScript. Arrays are essentially objects with a few special methods and a length property. You can declare an array like this: [undefined] and it's valid; that's an array with one item. Iterating over your array should absolutely include this item.

-Aaron

Sorry for any typos. Big fingers , tiny buttons.

On Dec 14, 2009, at 7:52 PM, Roman Land <[email protected]> wrote:

Thats true, it does look better.

About who's fault it is, I would expect a "foreach" loop not to try to itterate over an non existant element (at position 0 or whatever), the fact I am trying to use this nonexistent element later is not so evil IMO :)

for ( var thought in thoughts) { if (thought) alert("I think, therefor I exist!") }

On Tue, Dec 15, 2009 at 2:32 AM, Aaron Newton <[email protected]> wrote:
this is by far the better option.

it's not IE's fault that you are trying to reference something that doesn't exist. This isn't a bug in IE or MooTools.


On Mon, Dec 14, 2009 at 7:09 PM, Barry van Oudtshoorn <[email protected] > wrote:
What about doing something like

$each(arr, function(item) {
  if (item && item.foo) item.foo();
})

It's more robust and will mean that you can start indexing your arrays from whatever you want.


On 15/12/09 08:04, Roman Land wrote:

Indeed this code would cause an issue for me, since the issue is not due to referencing of the nonexistent item, rather inside the loop I do something like:

$each(arr, function(item, i) {
item.foo(); // this will throw an error on undefiled object and stop JS
})

My workaround by the way is to check weather i == 0 (this is a special array I use where I normally start at position 1).

FF's implementation does actually jump over position 0 - starting at 1, that would be logical interpretation of "foreach" vs "for (i =0 ; i < smt.length ; i++)" - where I tell him to begin at position 0 explicitly.

Cheers
-- Roman

On Tue, Dec 15, 2009 at 1:18 AM, Aaron Newton <[email protected]> wrote: Here is the code for forEach, implemented into browsers that do not implement it themselves:

 forEach: function(fn, bind){
for (var i = 0, l = this.length; i < l; i++) fn.call(bind, this [i], i, this);
 }

as you can see, it loops over each item and calls your function, passing the array's value at i. This is undefined for your zero value. I don't know where IE would freak out on this (though it doesn't surprise me that it might). The code above references yourArray[index] that shouldn't throw an error...

Aaron

On Mon, Dec 14, 2009 at 12:31 PM, Roman Land <[email protected]> wrote:
Lolz on the kindly:)

Paul, this site is meant to work on all browsers, I currently have a
work around, this behavior is undesired despite it's roots being in ie
implementation of foreach.

Cheers, Roman

On 14/12/2009, at 18:28, Paul Saukas <[email protected]> wrote:


> Roman ,
>
>     I believe that is an IE issue . I have no problem running your
> example on IE8 . It just kindly spits undefined out in place of the
> missing element 0 if i have it display the items, If i do the keys
> then IE shows 01234 and ff 1234. What version of IE are you using ?




--
---
"Make everything as simple as possible, but not simpler."

- Albert Einstein



--
Not sent from my iPhone.




--
---
"Make everything as simple as possible, but not simpler."

- Albert Einstein

Reply via email to