假设我有一个功能:
function Control(value){
var self=this;
self.param1=value;
self.param2=value.text;
}
是否可以在 JavaScript 中获取该函数创建的所有实例?
假设我有一个功能:
function Control(value){
var self=this;
self.param1=value;
self.param2=value.text;
}
是否可以在 JavaScript 中获取该函数创建的所有实例?
在不修改类的情况下,您几乎无能为力。如果您可以修改它,只需保留您构造的每个实例的引用:
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() 的任何实例的内存。
我在我的一个项目中使用了类似的东西,因为我需要为所有实例调用一个方法......
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对不起英语不好... :)
正如@Ian 在他的评论中建议的那样,您可以这样做:
// global are bad though!
var controlInstances = [];
function Control(value){
// track instances
controlInstances.push(this)
this.param1 = value;
thsi.param2 = value.text;
}
我在我的一个项目中使用了类似的东西,因为我需要为所有实例调用一个方法......
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对不起英语不好... :) 再见!
只需将其存储在全局数组中即可。将类似于一些静态模式。
var controls = [] ; //Global array
function Control(value){
var self = this;
self.param1=value;
self.param2=value.text;
controls.push(self);
}
var foo = new Control('bar');
console.log(foo.param1);