Objects

The following is the Codecademy introduction to objects in Javascript. It makes sense, but I'm not sure I "get it" in a usable sense. Once we're done with the lesson I'll try to write my own definition - one I actually grasp, or maybe the one below will make more sense.

Let's go back to the analogy of computer languages being like regular spoken languages. In English, you have nouns (which you can think of as "things") and verbs (which you can think of as "actions"). Until now, our nouns (data, such as numbers, strings, or variables) and verbs (functions) have been separate. No longer! Using objects, we can put our information and the functions that use that information in the same place.

Object Syntax

The basic syntax for an object includes the object name (as a container) and the objects key and value pairs.

var myObject = {
    key: value,
    key: value,
    key: value
};

Object Literal Notation

This is the first of two ways to write an object. It's what we see in the above example; with real values it might look something like this:

var object1 = {
    animal: "cat",
    breed: "siamese",
    age: 3
};

Object Constructor

The object constructor looks a little different. Its design follows the familiar pattern of functions.:

var something = new object();

Adding keys and values in two ways, both work just fine, but the second is shorter:

Standard Keys

var object2 = new Object();

object2["animal"] = "dog";
object2["breed"] = "shnautzer";
object2["age"] = 5;

Shorthand Keys

var object3 = new Object();

object3.animal = "cow";
object3.breed = "jersey";
object3.age = 10;

Using an object in an array

You can add an object in an array if you want. Using the above example this is what it would look like:

//here we create the object
var myObject = new Object();
//here we add its values
object3.animal = "cow";
object3.breed = "jersey";
object3.age = 10;
//here we list it as port of an array
var myArray (1,true,"3",myObject);

Another Object Example containing an array

var myLunch = new Object();

myLunch.meat = "T-Bone";
myLunch.sides = ["Rice Pilaf","Creamed Spinach","Candied Carrots"];
myLunch.drink = "Appletizer";
myLunch.dessert = "Custard over Malva Pudding";