在 javascript 中查找某个原型有多少个实例
Finding how many instances there are of a certain prototype, in javascript
如何找到某个原型有多少个实例?例如,我在 javascript 中定义了一个原型:
var foo = function(config) {
this.x = config.x;
...
}
foo.prototype.update = function() {
...
}
var f = new foo(...);
var c = new foo(...);
...
我怎样才能做到 returns2 的函数,因为有 2 个 foo 实例:f 和 c?
你可以试试这个
var counter = 0;
for (prop in foo.prototype) {
console.dir(prop);
counter++;
}
console.log('total prototypes in foo: '+counter);
更新
我想你想搜索 foo 的实例?
f instanceof foo
假设
var foo = function(config) {
this.x = config.x;
}
var config = new Object();
config.x = "1";
var f = new foo(config);
然后
f instanceof foo
true
那么如果你想在全局范围内找到 foo 的所有实例...
var counter = 0; //set the counter to zero
for (name in window) { //for property in global scope iteration
if (window[name] instanceof foo) { // if property is an instance of foo then count it
counter++;
}
}
console.log('total instances of foo '+counter); //total count
我刚刚尝试了这个并将 foo 换成 Object 并制作了一些添加到计数中的新对象。我不得不稍微修改一下。希望这现在有意义。
充分利用面向对象编程。
看这个例子PLNKR
您可以像这样创建 class 到 foo
的父级:
function Bar() {
this.foobars = [];
console.log('new Bar',this.foobars);
}
Bar.prototype.add = function(foo) {
console.log("Pushing");
this.foobars.push(foo);
console.log(this.foobars);
}
Bar.prototype.size = function(foo) {
return this.foobars.length;
}
function Foo(config) {
this.x = config.x;
this.add(this);
console.log('new Foo', Foo.prototype.foobars);
}
Foo.prototype = new Bar();
var f = new Foo({x:1});
var c = new Foo({x:2});
console.log("f.x:",f.x,"c.x:",c.x,"Foos: ",f.size());
并且 Foo 的每个实例都可以访问 Bar 中的整个 Foos 数组。如果你愿意,你也可以简单地使用一个计数而不是一个数组。
运行此代码时,控制台输出如下:
new Bar []
Pushing
[Foo]
new Foo [Foo]
Pushing
[Foo, Foo]
new Foo [Foo, Foo]
最终的控制台输出将是:
f.x: 1 c.x: 2 Foos: 2
如果您编写的是纯 javascript,而不是实际的处理代码,只需使用 JavaScript 中的原型概念:
// objects conventionally use CamelCase
var Foo = function(config) {
this.instantiated();
...
};
var fooPrototype = {
instances: 0,
instantiated: function() {
FooPrototype.instances++;
},
update: function() {
...
},
...
};
Foo.prototype = fooPrototype;
var f = new Foo();
console.log(f.instances); // -> "1"
var c = new Foo();
console.log(c.instances); // -> "2"
因为对象从不创建局部 instance
变量,隐藏原型值,所以每次修改都会修改原型变量,而原型变量只有一个。
另一方面,如果您使用的是真正的 Processing,则只需创建一个静态 class 变量并在构造函数调用期间递增该变量。
class Thing {
static int instances = 0;
Thing() {
Thing.instances++;
}
int getInstanceCount() {
return Thing.instances;
}
}
如何找到某个原型有多少个实例?例如,我在 javascript 中定义了一个原型:
var foo = function(config) {
this.x = config.x;
...
}
foo.prototype.update = function() {
...
}
var f = new foo(...);
var c = new foo(...);
...
我怎样才能做到 returns2 的函数,因为有 2 个 foo 实例:f 和 c?
你可以试试这个
var counter = 0;
for (prop in foo.prototype) {
console.dir(prop);
counter++;
}
console.log('total prototypes in foo: '+counter);
更新 我想你想搜索 foo 的实例?
f instanceof foo
假设
var foo = function(config) {
this.x = config.x;
}
var config = new Object();
config.x = "1";
var f = new foo(config);
然后
f instanceof foo
true
那么如果你想在全局范围内找到 foo 的所有实例...
var counter = 0; //set the counter to zero
for (name in window) { //for property in global scope iteration
if (window[name] instanceof foo) { // if property is an instance of foo then count it
counter++;
}
}
console.log('total instances of foo '+counter); //total count
我刚刚尝试了这个并将 foo 换成 Object 并制作了一些添加到计数中的新对象。我不得不稍微修改一下。希望这现在有意义。
充分利用面向对象编程。
看这个例子PLNKR
您可以像这样创建 class 到 foo
的父级:
function Bar() {
this.foobars = [];
console.log('new Bar',this.foobars);
}
Bar.prototype.add = function(foo) {
console.log("Pushing");
this.foobars.push(foo);
console.log(this.foobars);
}
Bar.prototype.size = function(foo) {
return this.foobars.length;
}
function Foo(config) {
this.x = config.x;
this.add(this);
console.log('new Foo', Foo.prototype.foobars);
}
Foo.prototype = new Bar();
var f = new Foo({x:1});
var c = new Foo({x:2});
console.log("f.x:",f.x,"c.x:",c.x,"Foos: ",f.size());
并且 Foo 的每个实例都可以访问 Bar 中的整个 Foos 数组。如果你愿意,你也可以简单地使用一个计数而不是一个数组。
运行此代码时,控制台输出如下:
new Bar []
Pushing
[Foo]
new Foo [Foo]
Pushing
[Foo, Foo]
new Foo [Foo, Foo]
最终的控制台输出将是:
f.x: 1 c.x: 2 Foos: 2
如果您编写的是纯 javascript,而不是实际的处理代码,只需使用 JavaScript 中的原型概念:
// objects conventionally use CamelCase
var Foo = function(config) {
this.instantiated();
...
};
var fooPrototype = {
instances: 0,
instantiated: function() {
FooPrototype.instances++;
},
update: function() {
...
},
...
};
Foo.prototype = fooPrototype;
var f = new Foo();
console.log(f.instances); // -> "1"
var c = new Foo();
console.log(c.instances); // -> "2"
因为对象从不创建局部 instance
变量,隐藏原型值,所以每次修改都会修改原型变量,而原型变量只有一个。
另一方面,如果您使用的是真正的 Processing,则只需创建一个静态 class 变量并在构造函数调用期间递增该变量。
class Thing {
static int instances = 0;
Thing() {
Thing.instances++;
}
int getInstanceCount() {
return Thing.instances;
}
}