简体   繁体   English

类的JavaScript实例

[英]JavaScript instances of a class

Let's say I have a function: 假设我有一个函数:

function Control(value){
    var self=this;
    self.param1=value;
    self.param2=value.text;
}

Is it possible to get all instances created by that function in JavaScript? 是否有可能在JavaScript中获得该函数创建的所有实例?

Without modifying the class, there is little you can do. 不修改类,您几乎无能为力。 If you can modify it, simply keep a reference of each instance you construct: 如果可以修改它,只需保留您构造的每个实例的引用:

function Control(value){
    Control.instances = Control.instances || [];
    Control.instances.push(this);

    var self=this;
    self.param1=value;
    self.param2=value.text;
}

// In case you are worried about garbage collection
Control.prototype.destroy = function() {
    var index = Control.instances.indexOf(this);
    Control.instances.splice(index, 1);
}


// Usage:
var x = new Control();
var y = new Control();
// do stuff with x and y
console.log('There are ' + Control.instances.length + ' instances of Control');
// all done with x and y
x.destroy();
x = null;
y.destroy();
y = null;
// no more references to the two instances, they can be garbage collected

Be warned though, you will prevent the garbage collector from freeing the memory of any instance you don't call destroy() on. 但是请注意,您将防止垃圾回收器释放任何未调用destroy()的实例的内存。

I used something similar in one of my projects because i needed to call a method for all the instances... 我在一个项目中使用了类似的东西,因为我需要为所有实例调用一个方法...

function Person(firstname, lastname) {
    var self = this; // For reference to "this" 
    this.firstname = firstname;
    this.lastname = lastname;


    document.addEventListener( "event/person", function( event ) {
        var params = event.detail.params;
        Person.prototype[ params.method ].call(self);

       // Take params:   params.params => ["param1", "param2", "param3"]
    });
}


Person.prototype.toString = function() {
    console.log("Firstname: " + this.firstname + " Lastname: " + this.lastname);  
};


Person.prototype.callAll = function( params ) {
    document.dispatchEvent( new CustomEvent( "event/person", {
        detail: {
            "params": params   
        }
    }));
};


var a = new Person("Gabriel", "Gatu");
var b = new Person("Marco", "Giovannini");


Person.prototype.callAll({
    method: "toString",
    params: ["param1", "param2", "param3"]
});

It uses the listeners to communicate between a simple method and the constructor class, also allows you to call whatever method you want contained in the prototype of the class, passing parameters, if necessary... 它使用侦听器在简单方法和构造函数类之间进行通信,还允许您调用要包含在类原型中的任何方法,并在必要时传递参数。

ps Sorry for the bad english... :) ps对不起英语不好... :)

Just store it in a global array. 只需将其存储在全局数组中即可。 Would be similar to some static pattern. 将类似于某些静态模式。

var controls = [] ; //Global array

function Control(value){
    var self = this;
    self.param1=value;
    self.param2=value.text;

   controls.push(self);
}

As @Ian suggested in his comment you can do it this way: 正如@Ian在他的评论中建议的那样,您可以通过以下方式进行操作:

// global are bad though!
var controlInstances = [];

function Control(value){
    // track instances
    controlInstances.push(this)
    this.param1 = value;
    thsi.param2 = value.text;
}

I used something similar in one of my projects because i needed to call a method for all the instances... 我在一个项目中使用了类似的东西,因为我需要为所有实例调用一个方法...

function Person(firstname, lastname) {
    var self = this; // For reference to "this" 
    this.firstname = firstname;
    this.lastname = lastname;


    document.addEventListener( "event/person", function( event ) {
        var params = event.detail.params;
        Person.prototype[ params.method ].call(self);

       // Take params:   params.params => ["param1", "param2", "param3"]
    });
}


Person.prototype.toString = function() {
    console.log("Firstname: " + this.firstname + " Lastname: " + this.lastname);  
};


Person.prototype.callAll = function( params ) {
    document.dispatchEvent( new CustomEvent( "event/person", {
        detail: {
            "params": params   
        }
    }));
};


var a = new Person("Gabriel", "Gatu");
var b = new Person("Marco", "Giovannini");


Person.prototype.callAll({
    method: "toString",
    params: ["param1", "param2", "param3"]
});

It uses the listeners to communicate between a simple method and the constructor class, also allows you to call whatever method you want contained in the prototype of the class, passing parameters, if necessary... 它使用侦听器在简单方法和构造函数类之间进行通信,还允许您调用要包含在类原型中的任何方法,并在必要时传递参数。

ps Sorry for the bad english... :) See ya ! ps对不起,英语不好... :)再见!

No. 没有。

The fact that you can only touch objects that you have explicit references to is part of JavaScript security model *. 您只能触摸具有明确引用的对象这一事实是JavaScript安全模型 *的一部分。

* That security model is not applicable in JavaScript in cases of global objects. * 对于全局对象,该安全模型不适用于JavaScript。

var foo = new Control('bar');
console.log(foo.param1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM