Online Book Reader

Home Category

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

By Root 1295 0
” earlier in this chapter.)

You can combine literal values and variables in one phrase if you want:

alert(“Hi there, “ + person + “!”);

The secret to this code is to follow the quotes. “Hi there, ” is a literal value because it is in quotes. On the other hand, person is a variable name because it is not in quotes; “!” is a literal value. You can combine any number of text snippets together with the plus sign.

Using the plus sign to combine text is called concatenation. (I told you it was a complicated word for a simple idea.)


Including spaces in your concatenated phrases

You may be curious about the extra space between the comma and the quote in the output line:

alert(“Hi there, “ + person + “!”);

This extra space is important because you want the output to look like a normal sentence. If you don’t have the space, the computer doesn’t add one, and the output looks like this:

Hi there,Rachael!

You need to construct the output as it should look, including spaces and punctuation.


Understanding the String Object

The person variable used in the previous program is designed to hold text. Programmers (being programmers) devised their own mysterious term to refer to text. In programming, text is referred to as string data.

The term string comes from the way text is stored in computer memory. Each character is stored in its own cell in memory, and all the characters in a word or phrase reminded the early programmers of beads on a string. Surprisingly poetic for a bunch of geeks, huh?


Introducing object-based programming (and cows)

JavaScript (and many other modern programming languages) uses a powerful model called object-oriented programming (OOP). This style of programming has a number of advantages. Most important for beginners, it allows you access to some very powerful objects that do interesting things out of the box.

Objects are used to describe complicated things that can have a lot of characteristics — like a cow. You can’t really put an adequate description of a cow in an integer variable.

In many object-oriented environments, objects can have the following characteristics. (Imagine a cow object for the examples.)

♦ Properties: Characteristics about the object, such as breed and age

♦ Methods: Things the objects can do, such as moo() and giveMilk()

♦ Events: Stimuli the object responds to, such as onTip

I describe each of these ideas throughout this minibook, as not all objects support all these characteristics.

If you have a variable of type cow, it describes a pretty complicated thing. This thing might have properties, methods, and events, all which can be used together to build a good representation of a cow. (Believe it or not, I’ve built cow programming constructs more than once in my life — and you thought programming was dull!)

Most variable types in Java are actually objects, and most JavaScript objects have a full complement of properties and methods; many even have event handlers. Master how these things work and you’ve got a powerful and compelling programming environment.

Okay, before you send me any angry e-mails, I know debate abounds about whether JavaScript is a truly object-oriented language. I’m not going to get into the (frankly boring and not terribly important) details in this beginner book. We’re going to call JavaScript object-oriented for now, because it’s close enough for beginners. If that bothers you, you can refer to JavaScript as an object-based language. Nearly everyone agrees with that. You can find out more information on this topic throughout this minibook while you discover how to make your own objects in Chapter 4 and use HTML elements as objects in Chapter 5.


Investigating the length of a string

When you assign text to a variable, JavaScript automatically treats the variable as a string object. The object instantly takes on the characteristics of a string object. Strings have a couple of properties and a bunch of methods. The one interesting property (at least for beginners) is length. Look at the example in Figure 1-7 to see the length property

Return Main Page Previous Page Next Page

®Online Book Reader