0

I am learning Javascript and have a question regarding objects. In a lesson I saw an object created like this:

var friends = {};

friends.john = {
    firstName: "John",
    lastName : "Doe",
    id       : 5566,
    fullName : function() {
       return this.firstName + " " + this.lastName;
    }
};

Is john in friends.john a key who's value is what's in the curly braces? I do not understand when an object is first created with the dot notation vs. just putting everything into the curly braces to begin with.

4
  • 4
    Try console.log for debugging, you'll see the structure of your object. Commented Jan 23, 2015 at 21:17
  • 1
    I agree with @elclanrs statement; the best way to learn is working through your problem and resolving an answer by yourself. Commented Jan 23, 2015 at 21:18
  • You very well can add john property within definition of friends, the magic in friends.john = {...} is, that you can add properties to an object at any time after it has been defined. Commented Jan 23, 2015 at 21:21
  • friends.john (or friends['john']) is how you access/set a key after the object has been declared. Setting it is the same as friends = {john: {}}. Commented Jan 23, 2015 at 21:21

4 Answers 4

3

Yes, the code you have there is exactly equivalent to:

var friends = {
    john: {
        firstName: "John",
        lastName : "Doe",
        id       : 5566,
        fullName : function() {
           return this.firstName + " " + this.lastName;
        }
    }
};

There are a wide variety of ways to do the same thing in different ways in JavaScript. In this case, you can use either syntax, but you can only use the curly brace syntax to create a new object. To assign a property to an existing object, you would typically use the assignment syntax:

friends.john = ...;
Sign up to request clarification or add additional context in comments.

Comments

1

john is a property of friends with the value of the object containing all of john's information. The dot notation is usually for use after the fact or in reference. You could have easily avoided it with this:

var friends = {
 john: {
  firstName: "John",
  lastName : "Doe",
  id       : 5566,
  fullName : function() {
   return this.firstName + " " + this.lastName;
  }
 }
};

2 Comments

I fixed a syntax error for you. You need the : inside the object, not = :-)
@RocketHazmat - Yes thank you, that was a slight copy paste error :P
0

Your code is equivalent to

var friends = {
    john: {
        firstName: "John",
        lastName : "Doe",
        id       : 5566,
        fullName : function() {
            return this.firstName + " " + this.lastName;
        }
    }
};

and also to

var friends = new Object();
friends.john = new Object();
friends.john.firstName = "John";
friends.john.lastName  = "Doe";
friends.john.id        = 5566;
friends.john.fullName  = function() {
    return this.firstName + " " + this.lastName;
};

Comments

0
code1:
var friends = {};
friends.john = {
firstName: "John",
lastName : "Doe",
id       : 5566,
fullName : function() {
   return this.firstName + " " + this.lastName;
}
};

here code1 is equivalent to code2:

 code2
var friends = {
john: {
    firstName: "John",
    lastName : "Doe",
    id       : 5566,
    fullName : function() {
       return this.firstName + " " + this.lastName;
    }
}
};

so if you run below statement both give the same result i.e. "John Doe"

alert(friends.john.fullName());

Note: dot notation is used to access object property.in your case friends is a object where John is a property of friends and the things in a curly braces is John value.

1 Comment

@Andy may this help you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.