Literal Notation vs Object Constructor Functions


#1


Hello,
So I am completing the "contact list" project, but had some confusion around literal notation vs object constructor functions. Is there a way to rewrite my code so that it exclusively uses constructor functions? Is there a benefit to using one method over the other when creating objects? Which would be best practice for this mini-project? Thanks in advanced!


var friends = new Object()
    friends.bill = {
        firstName: "Bill",
        lastName: "Duck",
        number: "775-722-0102",
        address: ["One Microsoft Way", "Redmond, CA", "57483"]
    };
    
    friends.steve = {
        firstName: "Steve",
        lastName: "Goose",
        number: "775-930-2392",
        address: ["12374 Aquamarine", "Reno, NV", "89304"]
    };


#2

From the standpoint of writing literal objects, the literal notation is preferred, and simpler.

var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Duck",
        number: "775-722-0102",
        address: ["One Microsoft Way", "Redmond, CA", "57483"]
    },    
    steve: {
        firstName: "Steve",
        lastName: "Goose",
        number: "775-930-2392",
        address: ["12374 Aquamarine", "Reno, NV", "89304"]
    }
};

In a later lesson we will cover custom constructor functions whereby our constructor will act as a template for new objects of the same class. Right now, our objects are of the Object class, only.


#3

Thank you for the clarification!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.