Skip to content

Latest commit

 

History

History
97 lines (70 loc) · 1.13 KB

File metadata and controls

97 lines (70 loc) · 1.13 KB

Objects


Object

Declaration

var hero = {
  breed: "Turtle",
  occupation: "Ninja",
  'finger count':  3
};

Object

Get properties

hero.occupation; // "Ninja" - dot notation
hero["breed"]; // "Turtle" - square bracket notation
hero["finger count"]; // 3
var key = "occupation";
hero[key]; // "Ninja"
hero.height; // "undefined"

Set properties

hero.name = "Michelangelo";
hero.name; // "Michelangelo"
hero["height"] = 1.5;
hero.height; // 1.5
var key = "occupation";
hero[key] = "Pizza eater";
hero.occupation; // "Pizza eater"

Objects

Passing objects by reference

var original = { howmany: 1 };
var copy = original;
copy.howmany; // 1
copy.howmany = 100;
original.howmany; // 100

Objects

Comparing objects

When you compare objects,
you'll get true only if you compare
two references to the same object.

var fido  = {breed: 'dog'};
var benji = {breed: 'dog'};

benji === fido // false
var mydog = benji;
mydog === benji // true
mydog === fido  // false