1

This is an example of code, from a book called JavaScript: The Definitive Guide, 6th Edition, that I do not understand.

He is talking about Objects.

var book ={
    topic: "javascript",
    fat: true
};
book.topic              => "javascript"
book.["fat"]            => True
book.author="flanagan"; // creates new property
book.contents= {};      // empty object*

So what I don't understand is the last part. Is he adding in a new property called "contents" that is empty? Because he is calling it an object and it's confusing me.

2
  • No, he is adding a new property called contents and is assigning it a new Object with no properties. Commented Nov 1, 2013 at 9:03
  • book.["fat"] will give you SyntaxError. Use either dot or square brackets but not both of them simultaneously. Commented Nov 1, 2013 at 9:04

8 Answers 8

2

Yes {} is an empty object in javascript which is being assigned to the contents property of the book object.

Here you can see that we can use functions defined globaly on object such as toString()

Sign up to request clarification or add additional context in comments.

Comments

1

Take a look at the way the book variable is defined. It is an Object declaration. In the last line, we add a property named contents and assign it an object declared exactly the same way as book except that this one has no property and therefore is an empty object : {}

Comments

1

The curly bracket { } notation denotes an object literal.

The fact that there is nothing between them means that a new object reference pointing to a blank object (inheriting from the base object) is stored in the book.contents property.

Comments

1

{} create an empty object. Means now this object can have new property same as defined by book.author="flanagan";

// Both are same
book.contents = new Object();
book.contents = { } ;

Comments

1

In JavaScript you can create an object with {} notation, called object literal and you can add what ever you want to this object in future by dot even a function , in your example in first step:

var book ={topic: "javascript",fat: true};

book object is created with two property topic and fat and then this object is extended with author as String and contents as an inner object, as I said you can use it for creation empty objects (var t={} //example); and if you use

typeof book.contents // returns "object"

Comments

0

It's creating a new property of the original book object, and setting the value of that property to a new empty object.

Comments

0

He's adding a new property called contents of the object book which itself is an empty object.

Here is a structure of what it would look like when he's made his changes to help you visualise it.

var book = {
    topic: "javascript",
    fat: true,
    author : "flanagan",
    contents : {}
};

2 Comments

So is there another way to say that a property has no value? If one just typed "contents" instead of "contents : {}"
You can assign object properties in two ways. book["contents] = or book.contents = . The value you assign to it can differ from a function to an empty object or array to a string or number. book.contents = "" now book.contents is a blank string.
0
<script type="text/javascript">

        var book ={
            topic: "javascript",
            fat: true
        };


        book.topic = "javascript";
        book.fat = true;
        book.author = "flanagan"; // creates new property
        book.contents = {};      // empty object*
        //The curly bracket { } denotes here an object.
        //will add a property contents and will contain an empty object

        alert(book.contents);   //it will display [object Object]

        book.contents.name = 'Success Stories';
        //adding propertis to the object contents
        alert(book.contents.name);

        /*
        you can consider it as object inside object

        objectOuter {
            property1: value1
            property2: value2
            property3: objectInner {
                property1: value1
                property2: value2
            }
        }
        */       

    </script>

Comments

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.