Skip to content

Commit

Permalink
add js example
Browse files Browse the repository at this point in the history
  • Loading branch information
robynitp committed Feb 4, 2015
1 parent 96f8f6d commit c69c577
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions week1/02javascript/object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// the object allColors has 3 properties, "red", "green", and "blue"
// each property contains an object with 2 properties, "title" and "hexcode"
var allColors = {
red: {
title: "Red as in Crimson",
hexcode: "#ff0000"
},
green: {
title: "Green as in Kale",
hexcode: "#00ff00"
},
blue: {
title: "Blue as in Sky",
hexcode: "#0000ff"
},
};

console.log("The object containing all the color objects:");
console.log(allColors); // outputs the entire object

// when the name of the property is a string, use the dot (.) syntax
console.log(allColors.blue); // outputs the object associated with the property "blue"

// when the property name is the value of a variable, use square brackets, []
var color = "red";

var chosenColorObject = allColors[color];

console.log("The object with the property name, " + color + ":");
console.log(chosenColorObject); // outputs the object associated with the property "red"

0 comments on commit c69c577

Please sign in to comment.