[OT] javascript guru out there?

Mike Park emparq at gmail.com
Thu Jul 22 18:30:12 UTC 2010


Hi Mike,

> I've been developing a javascript application that works everywhere
> except on Google's Chrome browser and have tracked it down to a "for
> each" statement.

You don't need the 'each' keyword in the 'for' statement. And if
you're looking to iterate through an array, you're much better off
doing something like:

var x = [1, 2, 3];
var len = x.length;
var i;
for (i = 0; i < len; i++) {
  // do whatever you need to with x[i] here
}

or using a shorter-handed version like:

var x = [1, 2, 3];
for (var i = 0; i < x.length; i++) {
  // ...
}

...if you can't be bothered to declare the variables separately
(though for large arrays, note that the second version is slower).

And if you really need to iterate through a collection contained in an
object, you can do something like:

var foo = {
  key1: 'val1',
  key2: 'val2',
  // ...
};

// this iterates all properties of 'foo', including possibly inherited
properties
for (var i in foo) {

  // we only want local properties of 'foo', so check this before
continuing as expected
  if (foo.hasOwnProperty(i)) {
    // ...
  }

}


> If anybody wants to take a peek/stab at this I could sure use the help -
> I've been stumped on this for two days now.
>
>         http://www.pastebin.com/Pq5E8Tfe

...all of the above being said, you might find faster (and more
detailed help) on a site generally geared more towards these types of
problems (general programming problems) on a site like
stackoverflow.com.


--Mike


More information about the users mailing list