开发者

Array with all Instances of Objects created with the same function constructor in Javascript

开发者 https://www.devze.com 2023-04-02 16:54 出处:网络
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.

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)


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"]


You can put your array as a static member of myObject:

function myObject (name) {
    this.name=name;
    this.init();
}
myObject.allInstances = [];
myObject.prototype = {
    init: function() {
        myObject.allInstances.push(this.name);
    }
};

I don't see where you are calling init(). I added a call to init() in the constructor.


It seems to me this would be easily done like so:

 var MyType = function(name)
 {
      this.name = name;
      MyType.Instances.push(this.name);
 };

 MyType.Instances = [];

 MyType.prototype.getInstances = function()
 {
     return MyType.Instances;
 };

 var obj = new MyType('Hello');
 var obj2 = new MyType('hello 2');

 console.log(obj2.getInstances());


Would this do?

function myObject(name) {
    this.name = name;
    this.constructor.allInstances.push(this.name);
}

myObject.allInstances = [];

object1 = new myObject("object1");
object2 = new myObject("object2");
console.log(myObject.allInstances);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号