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

  • Home
  • SEARCH
  • 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 am getting a little confused and need some help please. Take these two
My javascript file is getting pretty big (3000+ lines) and I'm getting confused as
I'm getting confused with the include/exclude jargon, and my actual SVN client doesn't seem
Im trying to get into some basic JavaFX game development and I'm getting confused
Without getting a degree in information retrieval, I'd like to know if there exists
I am very confused over something and was wondering if someone could explain. In
So I'm getting really confused on how to do this whole thing and I
getting confused here. I'm trying to install a second instance of reporting service sql
I am really getting confused on how pointers work. I am trying to write
I am getting confused as to what is the difference between the compiler and

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.