arozwalak
7/2/2015 - 12:56 PM

Javascript: Objects

Javascript: Objects

creating object

var empty_object = {};
var stooge = {
  "first-name" : "Jerome",
  "last-name" : "Howard"
};

objects can nest

var flight = {
  airline: "Oceanic",
  number: 815,
  departure: {
    IATA: "SYD",
    time: "2004-09-22 14:42",
    city: "Los Angeles"
  },
  arrival: {
    IATA: "LAX",
    time: "2004-09-23 10:42",
    city: "Los Angeles"
  }
};

retrieve from object

stooge["firs-name"]  // "Jerome"
flight.departure.IATA // "SYD"

var middle = stooge["middle-name"] || "(none)";
var status = flight.status || "unknown";

values from undefined will throw a TypeError exception

flight.equipment                           //undefined
flight.equipment.model                     //throw "TypeError"
flight.equipment && flight.equipment.model // undefined

update property value

stooge['first-name'] = 'Jerome';

if object does not have property, the object is augmented

stooge['middle-name'] = 'Lester';
stooge.nickname = 'Curly';
flight.equipment = {
     model: 'Boeing 777'
};
flight.status = 'overdue';

reference

var x = stooge;
x.nickname = 'Curly';
var nick = stooge.nickname; //nick is 'Curly' because x and stooge are references to the same object

var a = {}, b = {}, c = {}; // refers to different empty objects
var a = b = c = {};         // refers to the same empty object

Prototype

if (typeof Object.create !== 'function') {
  Object.create = function (o) {
    var F = function() {};
    F.prototype = o;
    return new F();
  };
}
var another_stooge = Object.create(stooge);

// when we make changes to an object, it's prototype is not touched
another_stooge['first-name'] = 'Harry';
another_stooge['middle-name'] = 'Moses';
another_stooge.nickname = 'Moe';

// Delegation: Prototype link is used only in retrieval. if we try to retrieve a property value that is not exists, JS attempts to retrieve the property value from the prototype object until the process finally bottoms out with Object.prototype. If it not exists result is undefined.

// prototype is dynamic relationship
stooge.profession = 'actor';
another_stooge.profession // 'actor'

reflection

typeof flight.number // 'number'
typeof flight.status // 'string'
typeof flight.arrival // 'object'
typeof flight.manifest // 'undefined'

flight.hasOwnProperty('number') // true
flight.hasOwnProperty('constructor') // false

enumeration

var name;
for (name in another_stooge) {   // for in loop don't guarantee to show names in order, you can avoid this with for loop (see below)
  if (typeof another_stooge[name] !== 'function') {
    document.writeln(name + ': ' + another_stooge[name]);
  }
}

var i;
var properties = [
  'first-name',
  'middle-name',
  'last-name',
  'profession'
];
for (i = 0; i < properties.length; i += 1) {
  document.writeln(properties[i] + ': ' +
                   another_stooge[properties[i]]);
}

Delete

another_stooge.nickname // 'Moe'
delete another_stooge.nickname;
another_stooge.nickname // 'Curly' from its prototype

Global Abatement

// to minimize the use of global variables is to create a single global variable for your application
var MYAPP = {};

MYAPP.stooge = {
  'first-name' : "Joe",
  'last-name' : "Howard"
};

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"
  }
};