// from Crockford's Javascript: The Good Parts
// Chapter 3
// playing with object literals
// an object literal is a pair of curly braces
// surrounding zero or more name/value pairs; it
// can appear anywhere an expression can appear
// here, quotes are required around "first-name"
// but not first_name; quotes are not required if
// a property name is a legal JS name and not
// a reserved word
// GLOBAL ABATEMENT
// JS makes it easy to define global variables
// that hold all the assets of your app.
// But they weaken the resiliency of programs
// To minimize the use of globals, create a single
// global variable for your app:
// WHY? By reducing to a single name, you reduce
// the chance of bad interactions with other apps,
// widgets, or libraries AND becomes easier to
// read because it is obvious that MYAPP.stooge
// refers to a top-level structure.
var MYAPP = {};
MYAPP.stooge = {
"first-name": "Hank",
"last-name": "Quinlan"
};
MYAPP.flight = {
airline: "Oceanic",
number: 815,
departure: {
IATA: "SYD",
time: "2004-09-22 14:55",
city: "Sydney"
},
arrival: {
IATA: "LAX",
time: "2004-09-23 10:42",
city: "Los Angeles"
}
};
flight.status = 'overdue';
flight.equipment = {
model: 'Boeing 777'
};
//var status = flight.status || "unknown";
//window.alert(status);
// is set as unknown
// REFERENCE
// objects are passed around by reference.
// they are never copied.
var x = stooge;
x.nickname = 'Curly';
var nick = stooge.nickname;
// nick is 'Curly' because x and stooge
// are references to the same object
// REFLECTION
// window.alert(typeof flight.number);
// inspect an object to determine
// what properties it has
// returns number, string, object, undefined
// window.alert(flight.hasOwnProperty('number'));
// use a method to deal with undesired props
// returns true if the object has a particular
// property
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.