This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by Deric Cain
over 9 years

How to add keys/values to object with dot notation?

I am trying to use dot notation to add keys/values to my objects that I have created, but I cannot figure out how to do it. Here is my code that is not working:

    var friends = {};
    
    friends.steve = new Object();
    friends.bill = new Object();
    
    steve().firstName = 'Steve';
    steve().lastName  = 'Jobs';
    steve().number    = '555.555.1234';
    
    bill().firstName = 'Bill';
    bill().lastName  = 'Gates';
    bill().number    = '555.444.1234';

I have tried it without the parenthesis after the object name, but that does not work either.

Answer 540af39c631fe97824005cb1

3 votes

Permalink

Hi Deric, On lines two and three you have declared steve and bill as objects, but you also have declared them as properties (or keys) of object friends.

Because they are keys in friends you need to address them as such:

friends.steve.firstName = 'Steve';

friends.bill.firstName = 'Bill';

and so on…

points
Submitted by tony de araujo
over 9 years

1 comments

Deric Cain over 9 years

Thank you for the explanation! I really appreciate it.