17.11.2015 Views

JavaScript_Succinctly

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

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

The concept to take away here is that variables that point to a complex object in<br />

memory are equal only because they are using the same "address.” Conversely, two<br />

independently created objects are not equal even if they are of the same type and<br />

possess the exact same properties.<br />

Complex objects have dynamic properties<br />

A new variable that points to an existing complex object does not copy the object. This<br />

is why complex objects are sometimes called reference objects. A complex object can<br />

have as many references as you want, and they will always refer to the same object,<br />

even as the object being referenced changes.<br />

Sample: sample20.html<br />

<br />

var objA = { property: 'value' };<br />

var pointer1 = objA;<br />

var pointer2 = pointer1;<br />

// Update the objA.property, and all references (pointer1 and pointer2)<br />

are updated.<br />

objA.property = null;<br />

// Logs 'null null null' because objA, pointer1, and pointer2 all<br />

reference the same object.<br />

console.log(objA.property, pointer1.property, pointer2.property);<br />

<br />

This allows for dynamic object properties because you can define an object, create<br />

references, update the object, and all of the variables referring to the object will "get"<br />

that update.<br />

The typeof operator used on primitive and complex values<br />

The typeof operator can be used to return the type of value you are dealing with. But<br />

the values returned from it are not exactly consistent or what some might say, logical.<br />

The following code exhibits the returned values from using the typeof operator.<br />

37

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

Saved successfully!

Ooh no, something went wrong!