4 Replies - 325 Views - Last Post: 05 November 2013 - 04:06 PM

#1 apathybear7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-June 13

trouble understanding for-in loop and this page in my book

Posted 05 November 2013 - 02:04 PM

First off, I'm really new to Javascript and programming in general. I am reading HEAD First HTML 5 and the book is good so far. In this specific chapter some things seem rushed, so I am having trouble understanding for-in loop.

http://imageshack.us/photo/my-images/51/sok8.jpg/
I am having a hard time understanding specifically the alert and how it says "each time through the loop the variable prop gets the string value of the next property name"

What does this mean? They kind of just throw this in out of no where.

Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: trouble understanding for-in loop and this page in my book

#2 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 461
  • View blog
  • Posts: 3,160
  • Joined: 12-January 10

Re: trouble understanding for-in loop and this page in my book

Posted 05 November 2013 - 02:10 PM

i cant see the image-- why not just inbed it so everyone can see it
Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,228
  • Joined: 08-June 10

Re: trouble understanding for-in loop and this page in my book

Posted 05 November 2013 - 02:11 PM

let’s use an example
var fido = {};
fido.name = "Fido";
fido.type = "Dog";

for (var prop in fido) {
  console.log("Property name: '%s', Property value: '%s'", prop, fido[prop]);
}

// logs
Property name: 'name', Property value: 'Fido'
Property name: 'type', Property value: 'Dog'



the next level of for…in is to know what properties in a complex object are looped over and which not.
Was This Post Helpful? 1
  • +
  • -

#4 apathybear7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-June 13

Re: trouble understanding for-in loop and this page in my book

Posted 05 November 2013 - 03:40 PM

For the duration of the conversation let us use Dormilich's example.

Now what I am specifically confused about is this line in your code:
console.log("Property name: '%s', Property value: '%s'", prop, fido[prop]);



and more specifically what is throwing me off is the ending:
prop, fido[prop]);


why is
prop
there? and why is
fido[prop]
there? What exactly do those do? I believe this will solve my confusion.

I may be over thinking this.
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: trouble understanding for-in loop and this page in my book

Posted 05 November 2013 - 04:06 PM

A for-in loop iterates over the properties of a Javascript object.

In Dormilich's example, the properties of the object are name and type.

So, as stated above the for-in loop is iterating over the object's properties. What is saved in the prop variable within the loop is the name of the property, hence prop prints the property name each time through the loop; but to get the value of that property you need to access the object, fido, and give the name of the property in brackets ([]), hence fido[prop] gives the value of the fido object's prop property.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1