Online Book Reader

Home Category

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

By Root 1427 0
in action.

Figure 1-7: This program reports the length of any text.

That’s kind of cool how the program can figure out the length of a phrase. The cooler part is the way it works. As soon as you assign a text value to a variable, JavaScript treats that variable as a string, and because it’s a string, it now has a length property. This property returns the length of the string in characters. Here’s how it’s done in the code.

A property is used like a special subvariable. For example, person is a variable in the previous example. person.length is the length property of the person variable. In JavaScript, an object and a variable are connected by a period (with no spaces).

The string object in JavaScript has only two other properties (constructor and prototype). Both of these properties are needed only for advanced programming, so I skip them for now.


Using string methods to manipulate text

The length property is kind of cool, but the string object has a lot more up its sleeve. Objects also have methods (things the object can do). Strings in JavaScript have all kinds of methods. Here are a few of my favorites:

♦ toUpperCase() makes an entirely uppercase copy of the string.

♦ toLowerCase() makes an entirely lowercase copy of the string.

♦ substring() returns a specific part of the string.

♦ indexOf() determines whether one string occurs within another.

The string object has many other methods, but I’m highlighting the preceding because they’re useful for beginners. Many string methods, such as big() and fontColor(), simply add HTML code to text. They aren’t used very often because they produce HTML code that won’t validate, and they don’t really save a lot of effort anyway. Some other methods, such as search(), replace(), and slice(), use advanced constructs like arrays and regular expressions that aren’t necessary for beginners. (To find out more about working with arrays, see Chapter 4 of this minibook. You can find out more about regular expressions in Chapter 6.)

Don’t take my word for it. Look up the JavaScript string object in the Aptana’s online help (or one of the many other online JavaScript references) and see what properties and methods it has.

Like properties, methods are attached to an object by the period. Methods are distinguished by a pair of parentheses, which sometimes contain special information called parameters.

The best way to see how methods work is to look at some in action. Look at the code for stringMethods.html:

Figure 1-8 displays the output produced by this program.

Figure 1-8: String methods can be fun.

Here’s another cool thing about Aptana. When you type text, Aptana understands that you’re talking about a string variable and automatically pops up a list of all the possible properties and methods. I wish I had that when I started doing this stuff!

You can see from the preceding code that methods are pretty easy to use. When you have a string variable, you can invoke the variable name followed by a period and the method name. Some methods require more information to do their job. Here are the specifics:

♦ toUpperCase() and toLowerCase() take the value of the variable and convert it entirely to the given case. This method is often used when you aren’t concerned about the capitalization of a variable.

♦ indexOf(substring) returns the character

Return Main Page Previous Page Next Page

®Online Book Reader