JSFiddle - React, Tailwind, and code Playground

by Jonathan McGlone

JavaScript

// 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