2. More People


#1


Why can't the part between the comment lines work as well? They say "create an object called mary", and this is an object.


https://www.codecademy.com/en/courses/building-an-address-book/0/2?curriculum_id=506324b3a7dffd00020bf661


var bob = {
    firstName: "Bob",
    lastName: "Jones",
    phoneNumber: "(650) 777-7777",
    email: "bob.jones@example.com"
};


/*******/function mary (firstName, lastName, phoneNumber, email) {
    this.firstName = "Mary";
    this.lastName = "Johnson";
    this.phoneNumber = "(650) 888 - 8888";
    this.email = "mary.johnson@example.com";
};

var mary = new Object();/*******/

var mary = {
    firstName: "Mary",
    lastName: "Johnson",
    phoneNumber: "(650) 888 - 8888",
    email: "mary.johnson@example.com"
};

var contacts = new Array();

contacts[0] = bob;
contacts[1] = mary;

console.log(contacts[1].phoneNumber);


#2

To create an object called Mary, you already did this right.

but you went on creating another array rather than populating that object. You should just do thus

var mary = new Object ();
mary.firstName = "Mary",
mary.lastName = "Johnson",
mary.phoneNumber = "(650) 888-8888",
mary.email = "mary.johnson@example.com"

Hope this help! :smile:


#3

Thank you for replying! Looking at what you wrote I found that :

var mary = new Object();

should be:

var mary = new mary();

since I created a mary object and not an object named Object.


#4

No you are getting it wrong.

var mary = new Object();

means you created an object mary (you call it mary to keep track for your ease, you can call it anything from stranger to a,b ,c etc) but it is still empty.

so, you are filling it with property. firstname, lastname, etc. by:
mary.firstName = "Mary";
mary.lastname = "Johnson"


#5

Thank you, that makes sense! :slight_smile: But I created :

function mary (firstName, lastName, phoneNumber, email) {
this.firstName = "Mary";
this.lastName = "Johnson";

And because I used 'this.firstName' and not 'mary.firstName' I have to use var mary = new mary();


#6

Well, that's what I was really trying to say that @mashuk said.

Really, the objective was to show you the use of new Object() rather than use a constructor as you did.

Code on! @anielfeyt :smile:


#7

You can do a lot of thing. But the exercises is to learn step by step. If you already know about object, class, prototype etc etc then there's no point wasting other's time for asking this sort of question. The instruction is not asking for your intellect rather to follow it, to make it clear if you can understand the specific topic.
There is a section later on the exercise where you will get the chance to use all of these.


#8

I learnt that way in previous exercises but just didn't no why it did not want to work in this one. And since everybody used the second choice for the creating of the object and I couldn't find what I was looking for in the Topics, I had to ask. There is always more than one way!:slight_smile:

Thanks for the help @mashuk and @bayoishola20