Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3854564
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:38:32+00:00 2026-05-19T17:38:32+00:00

I am getting confused over which way I should be creating an object in

  • 0

I am getting confused over which way I should be creating an object in javascript. It seems there are at least two ways. One is to use object literal notation while the other uses construction functions. Is there an advantage of one over the other?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T17:38:32+00:00Added an answer on May 19, 2026 at 5:38 pm

    If you don’t have behaviour associated with an object (i.e. if the object is just a container for data/state), I would use an object literal.

    var data = {
        foo: 42,
        bar: 43
    };
    

    Apply the KISS principle. If you don’t need anything beyond a simple container of data, go with a simple literal.

    If you want to add behaviour to your object, you can go with a constructor and add methods to the object during construction or give your class a prototype.

    function MyData(foo, bar) {
        this.foo = foo;
        this.bar = bar;
    
        this.verify = function () {
            return this.foo === this.bar;
        };
    }
    
    // or:
    MyData.prototype.verify = function () {
        return this.foo === this.bar;
    };
    

    A class like this also acts like a schema for your data object: You now have some sort of contract (through the constructor) what properties the object initializes/contains. A free literal is just an amorphous blob of data.

    You might as well have an external verify function that acts on a plain old data object:

    var data = {
        foo: 42,
        bar: 43
    };
    
    function verify(data) {
        return data.foo === data.bar;
    }
    

    However, this is not favorable with regards to encapsulation: Ideally, all the data + behaviour associated with an entity should live together.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting confused in the doco how I should be setting up Ninject. I'm
getting a bit confused over this problem... I have an administrative part of my
I'm getting really confused over how to connect to MongoLab on Heroku. To connect
Should be an easy one and might have over complicated the title somewhat. I
I'm getting confused with the include/exclude jargon, and my actual SVN client doesn't seem
I'm getting confused by bank switching in PIC assembler... This works for putting a
I'm getting confused re the range of options for development & deploying some simple
I am getting confused between TCP being Connection oriented and UDP being connectionless so
I'm getting confused by pointers in objective-c. Basically I have a bunch of static
I am getting confused with size_t in C. I know that it is returned

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.