23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 3 ■ OPERATORS<br />

on my part. JavaScript never wastes time and memory doing that. Moreover, if you compare an array to a<br />

function, === does not return false (or !== true) because of those being different subtypes. Rather, the<br />

boolean verdict simply derives from the array and function being in different locations in memory.<br />

Remember, JavaScript stores string, number, boolean, undefined, and null values in a different way to<br />

object, array, and function values (as we inferred earlier in the chapter).<br />

Now don’t be rolling your eyes at me. It’s vital to get this point. So, let’s compare some of the<br />

identical arrays in the following dough object representing the recipe for another of my favorite scones,<br />

hazelnut cherry, with === and !== like so in Firebug. As Figure 3–11 displays, === returns false and !==<br />

returns true for separate but identical arrays.<br />

var dough = {<br />

pastryFlour: [1 + 2/3, "cup"],<br />

hazelnutFlour: [1/3, "cup"],<br />

butter: [3, "tbs"],<br />

sugar: [2, "tbs"],<br />

seaSalt: [1/4, "tsp"],<br />

soda: [1/2, "tsp"],<br />

tartar: [1, "tsp"],<br />

heavyWhippingCream: [1, "cup"],<br />

currants: [1/3, "cup"]<br />

};<br />

dough.pastryFlour === [1 + 2/3, "cup"];<br />

// false<br />

dough.currants !== [1/3, "cup"];<br />

// true<br />

Figure 3–11. Separate but identical arrays are not equal.<br />

Separate but identical values of the object type or array and function subtypes are never equal. Like<br />

you and me, those are equal only to themselves, as the following sample and Figure 3–12 display:<br />

var dough = {<br />

pastryFlour: [1 + 2/3, "cup"],<br />

hazelnutFlour: [1/3, "cup"],<br />

butter: [3, "tbs"],<br />

sugar: [2, "tbs"],<br />

seaSalt: [1/4, "tsp"],<br />

soda: [1/2, "tsp"],<br />

73

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

Saved successfully!

Ooh no, something went wrong!