1

假设我有一个功能:

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

是否可以在 JavaScript 中获取该函数创建的所有实例?

4

7 回答 7

4

在不修改类的情况下,您几乎无能为力。如果您可以修改它,只需保留您构造的每个实例的引用:

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

但请注意,您将阻止垃圾收集器释放您未调用 destroy() 的任何实例的内存。

于 2013-10-04T14:45:33.240 回答
1

我在我的一个项目中使用了类似的东西,因为我需要为所有实例调用一个方法......

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"]
});

它使用侦听器在简单方法和构造函数类之间进行通信,还允许您调用类原型中包含的任何方法,必要时传递参数...

ps对不起英语不好... :)

于 2014-04-30T10:27:41.653 回答
0

正如@Ian 在他的评论中建议的那样,您可以这样做:

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

function Control(value){
    // track instances
    controlInstances.push(this)
    this.param1 = value;
    thsi.param2 = value.text;
}
于 2013-10-04T14:47:16.913 回答
0

我在我的一个项目中使用了类似的东西,因为我需要为所有实例调用一个方法......

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"]
});

它使用侦听器在简单方法和构造函数类之间进行通信,还允许您调用类原型中包含的任何方法,必要时传递参数...

ps对不起英语不好... :) 再见!

于 2014-04-30T10:42:56.053 回答
0

只需将其存储在全局数组中即可。将类似于一些静态模式。

var controls = [] ; //Global array

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

   controls.push(self);
}
于 2013-10-04T14:44:15.387 回答
-1

不。

您只能触摸您明确引用的对象这一事实是JavaScript 安全模型的一部分*.

*在全局对象的情况下,该安全模型不适用于 JavaScript。

于 2013-10-04T14:42:11.893 回答
-3
var foo = new Control('bar');
console.log(foo.param1);
于 2013-10-04T14:42:47.410 回答