> > From: Eridius
> > options =
> > {
> > var1: 'one',
> > var2: 'two'
> > }
> >
> > Is there a way to loop throught this object like I can 
> > do with .each? 

You can *use* $.each:

   $.each( options, function( key, val ) {
      console.debug( key, val );
   });

But beware! If your object happens to have a property named "length", $.each
will think it's an array and will do the wrong thing.

> From: Stephan Beal
> for( var key in obj ) {
>   var val = obj[key];
>  ...
> }

I also prefer the for loop in most cases. It is faster than $.each and just
as easy to use.

Another thing to beware of: If any code on your page has extended
Object.prototype, both of these techniques will fail. They will enumerate
the methods or properties that were added to Object.prototype along with the
ones you wanted. For example, add this code to your test and then try either
of those loops:

Object.prototype.foobar = function() {};

There is really no good solution for this other than to avoid extending
Object.prototype, although Douglas Crockford has some suggestions here:

http://yuiblog.com/blog/2006/09/26/for-in-intrigue/

-Mike

Reply via email to