假设我有一个功能:
function Control(value){
var self=this;
self.param1=value;
self.param2=value.text;
}
是否可以在JavaScript中获取该函数创建的所有实例?
答案 0 :(得分: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()的任何实例的内存。
答案 1 :(得分: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"]
});
它使用侦听器在一个简单的方法和构造函数类之间进行通信,还允许你调用你想要包含在类原型中的任何方法,如果需要的话传递参数......
P.S。对不起英语不好...... :)
答案 2 :(得分:0)
只需将其存储在全局数组中即可。类似于一些静态模式。
var controls = [] ; //Global array
function Control(value){
var self = this;
self.param1=value;
self.param2=value.text;
controls.push(self);
}
答案 3 :(得分:0)
正如@Ian在评论中建议你可以这样做:
// global are bad though!
var controlInstances = [];
function Control(value){
// track instances
controlInstances.push(this)
this.param1 = value;
thsi.param2 = value.text;
}
答案 4 :(得分: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"]
});
它使用侦听器在一个简单的方法和构造函数类之间进行通信,还允许你调用你想要包含在类原型中的任何方法,如果需要的话传递参数......
P.S。抱歉英语不好...... :)看见你!
答案 5 :(得分:-1)
答案 6 :(得分:-3)
var foo = new Control('bar');
console.log(foo.param1);