11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Declare a function taking two arguments, which it will modify<br />

function modifyValues(ref, prim)<br />

{<br />

}<br />

ref[0] = "changed"; // modify the first argument, an array<br />

prim = prim - 8; // modify the second, a number<br />

// Invoke the function<br />

modifyValues(refType, primType);<br />

// Print the value of the reference type<br />

document.writeln("<strong>The</strong> value of refType is: ", refType+"");<br />

// Print the value of the primitive type<br />

document.writeln("<strong>The</strong> value of primType is: ", primType);<br />

<strong>The</strong> result is shown in Figure 6-3. Notice how the value of the reference type changed but the<br />

value of the primitive type did not.<br />

Figure 6-3: <strong>Reference</strong> variables can be changed within functions.<br />

Comparing Objects<br />

Another situation where you need to be careful with reference types (objects) is when<br />

comparing them. When you use the equality (==) comparison operator, the interpreter<br />

compares the value in the given variables. For primitive types, this means comparing the actual<br />

data:<br />

var str1 = "abc";<br />

var str2 = "abc";

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

Saved successfully!

Ooh no, something went wrong!