04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5: Reference Types<br />

scope, whereas automatically created primitive wrapper objects exist for only one line of code before they<br />

are destroyed. This means that properties and methods cannot be added at runtime. Take this for example:<br />

var s1 = “some text”;<br />

s1.color = “red”;<br />

alert(sl.color); //undefined<br />

Here, the second line attempts to add a color property to the string s1 . However, when s1 is accessed<br />

on the third line, the color property is gone. This happens because the String object that was created<br />

in the second line is destroyed by the time the third line is executed. The third line creates its own<br />

String object, which doesn ’ t have the color property.<br />

It is possible to create the primitive wrapper objects explicitly using the Boolean , Number , and String<br />

constructors. This should be done only when absolutely necessary, because it is often confusing for<br />

developers as to whether they are dealing with a primitive or reference value. Calling typeof on an<br />

instance of a primitive wrapper type returns “ object ” , and all primitive wrapper objects convert to the<br />

Boolean value true .<br />

Even though it ’ s not recommended to create primitive wrapper objects explicitly, their functionality is<br />

important in being able to manipulate primitive values. Each primitive wrapper type has methods that<br />

make data manipulation easier.<br />

The Boolean Type<br />

The Boolean type is the reference type corresponding to the Boolean values. To create a Boolean object,<br />

use the Boolean constructor and pass in either true or false as in the following example:<br />

var booleanObject = new Boolean(true);<br />

Instances of Boolean override the valueOf() method to return a primitive value of either true or<br />

false . The toString() method is also overridden to return a string of “ true ” or “ false ” when<br />

called. Unfortunately, not only are Boolean objects of little use in ECMAScript, they can actually be<br />

rather confusing. The problem typically occurs when trying to use Boolean objects in Boolean<br />

expressions, as in this example:<br />

var falseObject = new Boolean(false);<br />

var result = falseObject & & true;<br />

alert(result); //true<br />

var falseValue = false;<br />

result = falseValue & & true;<br />

alert(result); //false<br />

In this code, a Boolean object is created with a value of false . That same object is then ANDed with the<br />

primitive value true . In Boolean math, false AND true is equal to false . However, in this line of<br />

code, it is the object named falseObject being evaluated, not its value ( false ). As discussed earlier, all<br />

objects are automatically converted to true in Boolean expressions, so falseObject actually is given a<br />

value of true in the expression. Then, true ANDed with true is equal to true .<br />

131

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

Saved successfully!

Ooh no, something went wrong!