Jump to content

More Examples for For...If Statements


violagirl
 Share

Recommended Posts

Sorry I messed up the title! I meant to type For...In Statements. Grah.The tutorial on this site says that a for...in statement can be used to loop through the elements of an array or the properties of an object. They gave an example with an array, but I can't figure out how one would use it for objects. Could somebody give me an example? Also, it said that the argument variable could be a named variable, an array element, or a property of an object. Could someone also give an example demonstrating how it would work if you used a property of an object? (this could probably be paired with the example I asked for above, I think)

Link to comment
Share on other sites

I actually would recomend not using it at all. you can do everything with a regualr for loop. Reason I say this is if you intend to use any JS toolkits or mess around with OO Js and protoyping then these things can cause the for in loops to nto work correctly.

Link to comment
Share on other sites

I actually would recomend not using it at all. you can do everything with a regualr for loop. Reason I say this is if you intend to use any JS toolkits or mess around with OO Js and protoyping then these things can cause the for in loops to nto work correctly.
Actually, thanks to this post (http://w3schools.invisionzone.com/index.php?showtopic=9628) I now understand one reason you'd want to use a for-in loop. It comes in handy if you need to build a clone method for an object. You can iterate through all the members in the object without having to know what those members are. With the typical for loop, you'd have to know what members an object had before you could iterate through them.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...