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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:58:10+00:00 2026-06-12T10:58:10+00:00

I know I can init an array of JS objects like this: var things

  • 0

I know I can init an array of JS objects like this:

var things = [
  {
    prop1: 'foo',
    prop2: 'bar'
  },
  {
    prop1: 'foo',
    prop2: 'bar'
  }
];

I guess I would call these ‘anonymous’ types (sorry, I’m using C#/.NET language).

What if I want these to be of the same prototype? So I have defined a constructor:

var Thing = function Thing() {
};

Thing.prototype.prop1 = 'default value';
Thing.prototype.prop2 = 'default value';

Now I want both the items in my original code above to be Things. Is there a good way to do this?

If I were to guess, I would say maybe something like:

var things = [
  new Thing() {
    prop1: 'foo',
    prop2: 'bar'
  },
  new Thing() {
    prop1: 'foo',
    prop2: 'bar'
  }
];

Which is basically C# object initializer syntax. What I’m trying to avoid is:

var thing1 = new Thing();
thing1.prop1 = 'foo';
thing1.prop2 = 'bar';
var thing2 = new Thing();
thing2.prop1 = 'foo';
thing2.prop2 = 'bar';
var things = [thing1, thing2];

Edit:

I should also note that my prototypes also contain some functions that are shared. Also in actuality I have arrays nested 3 deep, so its something more like:

{
   [
    { 
      [
        {},
        {}
      ]
    },
    {
      [
        {},
        {}
      ]
    }
  ]
}

Which is why I as hoping to just init everything inline like this and not setting each property line by line.

  • 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-06-12T10:58:11+00:00Added an answer on June 12, 2026 at 10:58 am
    var Thing = function(params) {
      this.prop1 = params.prop1;
      this.prop2 = params.prop2;
    };
    
    var things = [
      new Thing({
        prop1: 'foo',
        prop2: 'bar'
      }),
      new Thing({
        prop1: 'foo',
        prop2: 'bar'
      }),
    ];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I of course know that I can rename the init arg for an attribute
This is something I know can be done somehow , because I've done it
I'd like to know how to check an array that got several variables in
I am populating an array with string objects. There can be thousands of these.
I can add an element to a form like this: $form->addElement($element); However, that will
i have an array of custom objects. i'd like to be able to reference
Hello all and thanks for your time. I would like to know How I
I want to know can we have a JPanel with a Layout other than
I need to know can we add image in TextArea through StyleableTextField htmlText because
I have a query that I know can be done using a subselect, but

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.