Part 4


#1

var friends = new Object();

friends.bill = new Object();
bill.firstName: "Bill",
bill.lastName: "Gates",
bill.number: (206) 555-5555

friends.steve = new Object();
steve.firstName: "Steve",
steve.lastName: "Jobs",
steve.number: (405) 667-6666

Why would this be tossing a syntax error? Can you not use the object constructor?


#2

You can use the object constructor, but this:

steve.firstName: "Steve",

Is bad syntax.

Constructor syntax is:

steve.firstName = "Steve";

#3

@cadecodes

I changed it to this, but it still is not taking.

var friends = new Object();

friends.bill = new Object();
bill.firstName = "Bill";
bill.lastName = "Gates";
bill.number = (206) 555-5555

friends.steve = new Object();
steve.firstName = "Steve";
steve.lastName = "Jobs";
steve.number = (405) 667-6666


#4

Create the people, and then add them to friends once they're initialized.

Also, you forgot semicolons after the numbers, and the numbers should be strings.


#5

@cadecodes

Could you expand on what you mean by create the people? Isn't that what I am doing with friends.bill = new Object(); ?


#6

@cadecodes

I got it, but can you explain why it worked? Do the objects have to both be created before you add the keys?

var friends = new Object();

friends.bill = new Object();
friends.steve = new Object();

friends.bill.firstName = "Bill";
friends.bill.lastName = "Gates";
friends.bill.number= "(206) 555-5555";


friends.steve.firstName = "Steve";
friends.steve.lastName = "Jobs";
friends.steve.number = "(206) 555-5555";

#7

It works because 'bill' itself isn't a variable, but a key in friends. I prefer to create 'bill' then add it to friends later.


#8

Gotcha! Thanks for the replies! Very much appreciated!