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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:24:57+00:00 2026-05-24T05:24:57+00:00

So far I saw three ways for creating an object in JavaScript. Which way

  • 0

So far I saw three ways for creating an object in JavaScript. Which way is best for creating an object and why?

I also saw that in all of these examples the keyword var is not used before a property — why? Is it not necessary to declare var before the name of a property as it mentioned that properties are variables?

In the second and third way, the name of the object is in upper-case whereas in the first way the name of the object is in lower-case. What case should we use for an object name?

First way:

function person(fname, lname, age, eyecolor){
  this.firstname = fname;
  this.lastname = lname;
  this.age = age;
  this.eyecolor = eyecolor;
}

myFather = new person("John", "Doe", 50, "blue");
document.write(myFather.firstname + " is " + myFather.age + " years old.");

Second way:

var Robot = {
  metal: "Titanium",
  killAllHumans: function(){
    alert("Exterminate!");
  }
};

Robot.killAllHumans();

Third way — JavaScript objects using array syntax:

var NewObject = {};

NewObject['property1'] = value;
NewObject['property2'] = value;
NewObject['method'] = function(){ /* function code 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-24T05:24:58+00:00Added an answer on May 24, 2026 at 5:24 am

    There is no best way, it depends on your use case.

    • Use way 1 if you want to create several similar objects. In your example, Person (you should start the name with a capital letter) is called the constructor function. This is similar to classes in other OO languages.
    • Use way 2 if you only need one object of a kind (like a singleton). If you want this object to inherit from another one, then you have to use a constructor function though.
    • Use way 3 if you want to initialize properties of the object depending on other properties of it or if you have dynamic property names.

    Update: As requested examples for the third way.

    Dependent properties:

    The following does not work as this does not refer to book. There is no way to initialize a property with values of other properties in a object literal:

    var book = {
        price: somePrice * discount,
        pages: 500,
        pricePerPage: this.price / this.pages
    };
    

    instead, you could do:

    var book = {
        price: somePrice * discount,
        pages: 500
    };
    book.pricePerPage = book.price / book.pages;
    // or book['pricePerPage'] = book.price / book.pages;
    

    Dynamic property names:

    If the property name is stored in some variable or created through some expression, then you have to use bracket notation:

    var name = 'propertyName';
    
    // the property will be `name`, not `propertyName`
    var obj = {
        name: 42
    }; 
    
    // same here
    obj.name = 42;
    
    // this works, it will set `propertyName`
    obj[name] = 42;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Far as best practices are concerned, which is better: public void SomeMethod(string str) {
When I was learning .NET I saw it as a platform that runs my
First three elements of the background: I have created a Excel Template which is
The usual method, as far as I know, of referring to an object in
I have lots of Java enums that I persist with Hibernate. As far as
I'm working on a DotNetNuke module that includes a tree-style navigation menu. So far,
I saw a comment on another question (I forget which one) encouraging the asker
I'm making a javascript/canvas game and I saw this example on CSS Tricks. Here's
Thanks very much Winchester for the kind help! I also saw the tutorial and
As far as I know, foreign keys (FK) are used to aid the programmer

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.