Pro JavaScript Techniques (Resig): Chapter 2

sample code from the Apress Book

by nickadeemus2002

HTML

<div id="main-wrapper">
<p>i am inside the main wrapper</p>
<div id="body-wrapper">
    <p>i am inside the body wrapper</p>
</div>
</div>

CSS

div{padding:20px;}
#main-wrapper{background-color:#585858;}
#body-wrapper{ background-color:#eee;border:1px solid #ffffff;}
p{padding:15px;}
p:hover{cursor:pointer;}

JavaScript

/**
* js samples for resig book - chapter 2
*/
  
/**
* REFERENCES ONLY POINT TO THE FINAL REFERRED OBJECT, NOT A REFERENCE ITSELF!
* A REFERENCE TRAVERSES DOWN THE REFERENCE CHAIN AND ONLY POINTS TO THE CORE OBJECT.
*/


/**
* 2.1 object references to single object
*/
var obj = new Object();
var objRef = obj;
obj.myGirls=['Makayla', 'Kathryn'];
//console.log(objRef);


/**
* 2.2 Array() is a self-modifying object that creates gloabl updates with
*/
// an internal method...push().
var items = ['one', 'two', 'three'];
var itemsRef = items;
//update items so all references get update
items.push('four');
//console.log(itemsRef);


/**
* 2.3 Changing the reference of an Object while maintaining integrity
*/
var moreItems = ['four', 'five', 'six'];
var moreItemsRef = moreItems;
//set moreItems to a new array object
moreItems = ['seven','eight','nine'];
//moreItems and moreItemsRef now points to different objects, while
//moreItemsRef maintains reference to original moreItems object
//console.log(moreItems);
//console.log(moreItemsRef);


/**
* 2.4 Object modification resulting in a NEW OBJECT, not a self-modifying object
* modified string() creates NEW OBJECT.
*/
// Set stringItem equal to a new string object
var stringItem = "test";
// stringItemRef now refers to the same string object
var stringItemRef = stringItem;
// Concatenate some new text onto the string object
// NOTE: This creates a new object, and does not modify the original object.
stringItem += "ing";
// The values of stringItem and stringItemRef are NOT equal, as a whole
// new string object has been created
//console.log(stringItem)
//console.log(stringItemRef);




/**
* Funciton Overloading and Type Checking
*/

/**
* Functions have a contextual variable: arguments.  arguments is a pseudo
* array containing all the arguments passed into the function with access
* and .length.
*/


/**
* Function Overloading example
*/

//a simple function for sending a message...
function sendMessage( msg, obj ) {
  ...