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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:18:07+00:00 2026-05-25T11:18:07+00:00

In trying to built a object with a inner propriety in the constructor function

  • 0

In trying to built a object with a inner propriety in the constructor function that keeps the array with all the objects created with the same constructor.

I’m thinking that the best way would be with a closure on object initialization and this is how I try to solve this:


    function myObject (name){
        this.name=name;
        this.allInstances = [];
        }

    myObject.ptototype = {

        init : function(){
            return function(){this.allInstances.push(this.name)};
            }(),
        }   

    object1 = new myObject("object1");
    object2 = new myObject("object2");
    console.log(object1.allInstances); // should print ["object1", "object2"]

Does anyone know how to achieve that ? Is that even possible ?
I’m specifically trying to get a solution which uses only function constructor and prototype to achieve that.

I know how to solve that by pushing the proprieties to an external array, like:


    var allInstances = [];
    function myObject (name){
        this.name=name;
        allInstances.push(this.name);
        }
    console.log(allInstances)

  • 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-25T11:18:08+00:00Added an answer on May 25, 2026 at 11:18 am

    Place the Array as a property on the prototype, and it will be shared among all instances:

    function myObject(name) {
        this.name = name;
        this.allInstances.push( this.name );
    }
    
    myObject.prototype.allInstances = [];
    
    object1 = new myObject("object1");
    object2 = new myObject("object2");
    
    console.log(object1.allInstances); // ["object1", "object2"]
    

    Or if you want the Array to be more protected, use a module pattern, and include a function on the prototype to return the Array.

    var myObject = (function() {
        var allInstances = [];
    
        function func(name) {
            this.name = name;
            allInstances.push( this.name );
        }
    
        func.prototype.getAllInstances = function() { return allInstances; };
    
        return func;
    })();
    
    object1 = new myObject("object1");
    object2 = new myObject("object2");
    
    console.log(object1.getAllInstances()); // ["object1", "object2"]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use hibernates built in createsql function but it seems that it
I am trying to build an array that contains arrays of dictionary objects in
I have a Javascript object that I'm trying to use as a hashmap. The
I am trying to get meta-data of all built-in Clojure functions. In previous question
So I'm trying to learn Silverlight so I've built a simple demo app that
i am trying to attach a built context menu strip into an object which
I've created a C# COM object, and am trying to instantiate it from VBscript
Using Visual Studio 2008 I've built a COM object that targets the .Net Framework
I'm trying to create XML using the ElementTree object structure in python. It all
I am trying to create a dynamic table that is being built based on

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.