Online Book Reader

Home Category

HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [153]

By Root 1513 0

Introducing JSON notation


It doesn’t take long for your code to become complex. Soon enough, you find yourself wanting to write more sophisticated programs. When things get larger, you need new kinds of organizational structures to handle the added complexity.

You can bundle several lines of code into one container and give this new chunk of code a name: that is a function. You can also take a whole bunch of variables, put them into a container, and give it a name. That’s called an array. If you combine functions and data, you get another interesting structure called an object.

This chapter is about how to work with more code and more data without going crazy.


Breaking Code into Functions

Functions come in handy when you’re making complex code easier to handle — a useful tool for controlling complexity. You can take a large, complicated program and break it into several smaller pieces. Each piece stands alone and solves a specific part of the overall problem.

You can think of each function as a miniature program. You can define variables in functions, put loops and branches in there, and do anything else you can do with a program. A program using functions is basically a program full of subprograms.

After you define your functions, they’re just like new JavaScript commands. In a sense, when you add functions, you’re adding to JavaScript.

To explain functions better, think back to an old campfire song, “The Ants Go Marching.” Figure 4-1 re-creates this classic song for you in JavaScript format. (You may want to roast a marshmallow while you view this program.)

Figure 4-1: Nothing reminds me of functions like a classic campfire song.

If you’re unfamiliar with this song, it simply recounts the story of a bunch of ants. The littlest one apparently has some sort of attention issues (but we love him anyway). During each verse, the little one gets distracted by something that rhymes with the verse number. The song typically has ten verses, but I’m just doing two for the demo.


Thinking about structure

Before you look at the code, think about the structure of the song, “The Ants Go Marching.” Like many songs, it has two parts. The chorus is a phrase repeated many times throughout the song. The song has several verses, which are similar to each other, but not quite identical.

Think about the song sheet passed around the campfire. (I’m getting hungry for a S’more.) The chorus is usually listed only one time, and each verse is listed. Sometimes, you have a section somewhere on the song sheet that looks like the following:

Verse 1

Chorus

Verse 2

Chorus

Musicians call this a road map, and that’s a great name for it. A road map is a high-level view of how you progress through the song. In the road map, you don’t worry about the details of the particular verse or chorus. The road map shows the big picture, and you can look at each verse or chorus for the details.


Building the antsFunction.html program

Take a look at the code for antsFunction.html and see how it reminds you of the song sheet for “The Ants Go Marching”: