HTML and CSS Reference
In-Depth Information
Figure 9.26
Producing random numbers (Example 9.22).
provide properties and methods that can be defined for the object. For example, the
String
object has a number of methods that let you change the font color, size, and style
of a string; and the
Number
object has methods that allow you to format a number to a
specified number of significant digits. Whether you use the object or literal notation to
create a string, number, or Boolean, JavaScript handles the internal conversion between
the types. The real advantage to the wrapper object is its ability to apply and extend
properties and methods to the object, which in turn, will affect the primitive.
We have used strings throughout this topic. They were sent as arguments to the
write()
and
writeln()
methods, they have been assigned to variables, they have been concate-
nated, and so on. As you might recall, a string is a sequence of characters enclosed in
either double or single quotes. The
String
object (starting with JavaScript 1.1) is a core
JavaScript object that allows you to treat strings as objects. The
String
object is also
called a wrapper object because it wraps itself around a string primitive, allowing you to
apply a number of properties and methods to it.
You can create a
String
object implicitly by assigning a quoted string of text to a vari-
able, called a string primitive (see “Primitive Data Types” on page 53 of Chapter 3, “The
Building Blocks: Data Types, Literals, and Variables”), or by explicitly creating a
String
object with the
new
keyword and the
String()
object constructor method. Either way, the
properties and methods of the
String
object can be applied to the new string variable.