在 Javascript 中获取类的所有实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19526456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get all instances of class in Javascript
提问by Vincent Duprez
I thought there would already be an answer for this but I can't seem to find one.. How can I run a particular class method on all instances of this class in Javascript?
我以为已经有答案了,但我似乎找不到答案.. 如何在 Javascript 中对此类的所有实例运行特定的类方法?
This has to be done in a situation where I do not know the names of the instances. I think I could use some sort of static variable inside my class to store all instances, but this doesn't seem to exist in JS
这必须在我不知道实例名称的情况下完成。我想我可以在我的类中使用某种静态变量来存储所有实例,但这在 JS 中似乎不存在
So how to call my method on all existing instances of my class? Note : just for clarification : I'm not speaking about CSS classes, I'm speaking about objects.
那么如何在我的类的所有现有实例上调用我的方法呢?注意:只是为了澄清:我不是在谈论 CSS 类,我在谈论对象。
Edit : By Class in Javascript, I mean the creation of a new object on a function:
编辑:通过 Javascript 中的类,我的意思是在函数上创建一个新对象:
function something()
{
}
var instance = new something();
回答by SLaks
You can create a static array and store it on your constructor function:
您可以创建一个静态数组并将其存储在您的构造函数中:
MyClass.allInstances = [];
MyClass.allInstances.push(this);
However, you need some way to figure out when to remove instances from this array, or you'll leak memory.
但是,您需要某种方法来确定何时从该数组中删除实例,否则会泄漏内存。
回答by leaf
You'll have to provide a custom implementation.
您必须提供自定义实现。
I would do something like this :
我会做这样的事情:
function Class() {
Class.instances.push(this);
};
Class.prototype.destroy = function () {
var i = 0;
while (Class.instances[i] !== this) { i++; }
Class.instances.splice(i, 1);
};
Class.instances = [];
var c = new Class();
Class.instances.length; // 1
c.destroy();
Class.instances.length; // 0
Or like this :
或者像这样:
function Class() {};
Class.instances = [];
Class.create = function () {
var inst = new this();
this.instances.push(inst);
return inst;
};
Class.destroy = function (inst) {
var i = 0;
while (Class.instances[i] !== inst) { i++; }
Class.instances.splice(i, 1);
};
var c = Class.create();
Class.instances.length; // 1
Class.destroy(c);
Class.instances.length; // 0
Then you could loop through all instances like so :
然后你可以像这样循环遍历所有实例:
Class.each = function (fn) {
var i = 0,
l = this.instances.length;
for (; i < l; i++) {
if (fn(this.instances[i], i) === false) { break; }
}
};
Class.each(function (instance, i) {
// do something with this instance
// return false to break the loop
});
回答by Koas
Sorry for such a late reply, but I found myself trying to achieve this and I think this may be a simpler answer.
很抱歉这么晚才回复,但我发现自己正在努力实现这一目标,我认为这可能是一个更简单的答案。
Say you want all instances of class MyClass, only get instances created at top window level (not including instances created inside a closure):
假设您想要类 MyClass 的所有实例,只获取在顶层窗口级别创建的实例(不包括在闭包内创建的实例):
for (var member in window)
{
if (window[member] instanceof MyClass)
console.info(member + " is instance of MyClass");
}
回答by 0BLU
In Chrome 62+ you can use queryObjects
which will not workin native JavaScript code but in the console so it's great for debugging.
在 Chrome 62+ 中,您可以使用queryObjects
which在本机 JavaScript 代码中不起作用,但在控制台中使用,因此非常适合调试。
class TestClass {};
const x = new TestClass();
const y = new TestClass();
const z = new TestClass();
queryObjects(TestClass)
回答by olleicua
You'll need to store a list of instances yourself:
您需要自己存储实例列表:
function someClass(param) {
// add to all
if (this.constructor.all === undefined) {
this.constructor.all = [this];
} else {
this.constructor.all.push(this);
}
// set param
this.logParam = function() { console.log(param); };
}
var instance1 = new someClass(1);
var instance2 = new someClass(2);
for (var i = 0; i < someClass.all.length; i++) {
someClass.all[i].logParam();
}
If memory leaks are a concern then you can create a method for deleting instances when you are done with them:
如果内存泄漏是一个问题,那么您可以创建一个方法来在完成后删除实例:
function someClass(param) {
...
this.destroy = function() {
var all = this.constructor.all;
if (all.indexOf(this) !== -1) {
all.splice(all.indexOf(this), 1);
}
delete this;
}
}