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 6971265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:49:04+00:00 2026-05-27T16:49:04+00:00

I am new to javascript. As far as I can tell there are 5

  • 0

I am new to javascript. As far as I can tell there are 5 ways to make an object (really a struct I guess). I was wondering what the best way is. Thanks.

var makeOption = function(name, dataType){
    var option = {
        name: name,
        dataType: dataType
    };
    return option;
};

var makeOption2 = function(name, dataType){ 
    this.name = name;
    this.dataType = dataType;
};

function makeOption3(name, dataType){
    this.name = name;
    this.dataType = dataType;
};

var makeOption4 = function makeOption4Name(name, dataType){
    this.name = name;
    this.dataType = dataType;
};

var v1A = makeOption("hannah", "int");
var v1B = new makeOption("hannah", "int");

//var v2A = makeOption2("hannah", "int"); <- undefined.  
var v2B = new makeOption2("hannah", "int");

// var v3A = makeOption3("hannah", "int"); <- undefined.
var v3B = new makeOption3("hannah", "int");

// var v4A = makeOption4("hannah" ,"int"); <- undefined.
var v4B = new makeOption4("hannah" ,"int");

This is what is displayed in the firebug DOM Tab:

enter image description here

  • 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-27T16:49:05+00:00Added an answer on May 27, 2026 at 4:49 pm

    Whole chapters of books on JavaScript best practices have been written on this subject. That said: If you aren’t concerned about inheritance and aren’t going to be creating numerous copies of an object with methods, i.e., you are just creating a “struct”, then object literal notation, your first example, is the way to go. In that approach, you are using object literal notation, which is lightweight and fast. It doesn’t mess with the object prototype or require the use of the new operator.

    Start adding methods to your object, however, and the answer changes to “it depends.”

    By the way, you left out a couple of ways to create an object:

    var o = {};
    o.name = "hannah";
    o.dataType = "int";
    

    and, not recommended:

    var o = new Object();
    o.name = "hannah";
    o.dataType = "int";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm relatively new to Javascript and was wondering if there's a quick way to
As far as I can tell, there are two main ways of creating functions
I have some simple javascript that as far as i can tell should work
I am new to JavaScript OOP. Can you please explain the difference between the
I'm new at javascript and while there are many more complex solutions, I don't
As far as I can tell this only is only broken in Internet Explorer.
Friends, I'm new to both Javascript and Regular Expressions and hope you can help!
I've been teaching myself JavaScript and i'm still fairly new, i've tried to make
New to javascript/jquery and having a hard time with using this or $(this) to
New to javascript, but I'm sure this is easy. Unfortunately, most of the google

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.