[英]JavaScript instances of a class
假設我有一個函數:
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對不起英語不好... :)
只需將其存儲在全局數組中即可。 將類似於某些靜態模式。
var controls = [] ; //Global array
function Control(value){
var self = this;
self.param1=value;
self.param2=value.text;
controls.push(self);
}
正如@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 foo = new Control('bar');
console.log(foo.param1);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.