JavaScript Cheet Sheet

1.6 - JS Objects General Overview

// Declare and initialize an object variable person
var person = {first_name: "John", middle_name: "Peter", last_name: "Smith", age: "40"};
var count = 0;
var myWindow = window.open("", "MsgWindow", "width=500,height=500");

// attr is a variable, in is a keyword
// for loop below will execute for every attribute inside the person object
myWindow.document.writeln("Displaying the different attributes inside person object < br>");
for (attr in person) {
// Output the attribute 'attr' each time the for loop is executed, which is counted with variable count    myWindow.document.write(count +" " +attr +"< br>");
   count++;
}
myWindow.document.write("< br>");
myWindow.document.writeln("Displaying the different values of the attributes inside person object < br>");
for (attr in person){
// Same for loop as above, but this time the output is person[attr] which will output the value of the attribute
   myWindow.document.writeln( person[attr] +"< br>");
}