ZAKAS OOP CHAPTER THREE : understanding objects

Notes and demo from Zakas Book

by nickadeemus2002

HTML

<script src="https://getfirebug.com/firebug-lite-debug.js"></script>

JavaScript

/*
*************************************************
ZAKAS OOP CHAPTER THREE : understanding objects
*************************************************
*/
//js objects= hash maps of key/value pairs.
//access obj properties using either 
//dot notation or bracket notation with 
//a string identifier. add a property at any time. //remove a property at any time with delete //operator. check if property exists by using the //in operator on property name + object. 
//if property is an own property,
//you could use hasOwnProperty(), which exists 
//on every object. all object properties are
//enumerable by default--they appear in for-in //loop

/*
var person1 = {
    _name: "Nicholas",
    //ECMAScript 5
    get name() {
        console.log("Reading name");
        return this._name;
    },
    set name(value) {
        console.log("Setting name to %s", value);
        this._name = value;
    }
};
console.log(person1.name);
*/

/*
**********************************************
enumeration
**********************************************
*/
/*
var property;
for (property in window) {
console.log("Name: " + property);
console.log("Value: " +window[property]);
}
console.log(window.propertyIsEnumerable("Name"));
*/


/*
**********************************************
removing obj properties
**********************************************
*/
/*
//use the delete operator to completely 
//remove prop--calls internal[[Delete]]
var person1 = {
    name: "James"
};
console.log("name" in person1); 
//remove name
delete person1.name; 
console.log("name" in person1); 
console.log(person1.name);
*/

/*
**********************************************
detecting obj properties
**********************************************
*/
/*
// use in to test
var dog1 = {
    name: "Bella",
    age:"puppy",
    bark:function(){
        console.log('ruff ruff');
    }
};
console.log("name" in dog1);
console.log("age" in dog1); 
console.log("title" in dog1); 
dog1.bark();
console.log("bark" in dog1); 
//if...