23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 1 ■ REPRESENTING DATA WITH VALUES<br />

14<br />

To update the value of a member or to add a new member, you would use the = operator, same as<br />

before. Let’s add a bostonCreamPie member to iceCream. Then query its value, verifying our work with<br />

Figure 1–10:<br />

var iceCream = {<br />

chocolateFudgeBrownie: 4 * 260,<br />

halfBaked: 4 * 270,<br />

newYorkSuperFudgeChunk: 4 * 300,<br />

coffeeHeathBarCrunch: 4 * 280,<br />

cherryGarcia: 4 * 240,<br />

mudPie: 4 * 270,<br />

milkCookies: 4 * 270,<br />

cinnamonBuns: 4 * 290,<br />

chocolateChipCookieDough: 4 * 270,<br />

missionToMarzipan: 4 * 260<br />

};<br />

iceCream.bostonCreamPie = 4 * 250;<br />

iceCream.bostonCreamPie + " calories per pint";<br />

// "1000 calories per pint"<br />

Figure 1–10. Writing a new value to a member named with an identifier<br />

Creating an Array Literal<br />

The members in iceCream are coded like a top-ten list. However, there’s no way to have JavaScript query<br />

them that way. We couldn’t ask, “What’s my third favorite flavor?” for example. Plus, we have to name<br />

members and give them a value.<br />

So, if we wanted to simply create a top-ten list of flavors, omitting the depressing calorie details, an<br />

object wouldn’t do. For that we’d need an array, which is a subtype of the object value type. That is to<br />

say, an array is still an object; it just has some additional features.<br />

One of those features is ordering values numerically with non-negative integers beginning at 0.<br />

JavaScript does so behind the scenes. So, you just list values in an array from first to last; JavaScript takes<br />

care of the numbering. Note that numbered values are referred to as elements rather than members.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!