/*
*************************************************
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...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.