Javascript构造函数统计实例数
Javascript constructor function to count the number of instances
好的,所以我想在 javascript 中创建一个构造函数,它将计算使用此构造函数创建的实例总数。
var Component = function(name) {
this.name = name;
this.add = function(){this.prototype.countInstances++;};
this.add();
};
和
Component.prototype.countInstances=0;
据我理解正确,countInstances 变量被添加到原型中,它将作为所有实例的静态副本,并将作为我的计数器。
此代码的问题在于,由于我在构造函数之后声明了 countInstances,因此构造函数代码本身出现错误。如何纠正这个问题??
var ComponentCounter = 0;
var Component = function(name) {
this.name = name;
this.add = function(){this.prototype.countInstances++;};
this.add();
ComponentCounter++;
// or Component.counter++;
};
// or add it as a property of Component after it has been defined
// Component.counter = 0;
原型中的变量属于实例,因此您必须跟踪在实例之间持久保存的变量上的数据。
如果您希望 属性 附加到 class 本身,而不是 class 的实例,您不想添加 属性 原型:
var Component = function(name) {
this.name = name;
Component.instanceCount++;
};
Component.instanceCount = 0;
这样,您将每个名称分配给它的实例,并将实例总数分配给静态 class:
var foo = new Component('bar');
var baz = new Component('qux');
console.info(foo.name, baz.name, Component.instanceCount);
>> 'bar', 'qux', 2
As I understand it correctly, the countInstances variable is added to the prototype and will act like a static copy for all the instances and will act as my counter.
不,实际上它将是实例的 默认值,而不是 "static." 如果将它放在 Component.prototype
上,所有实例将通过原型链继承它,但通过实例更改它会给该实例它的 own 副本。示例:
var Foo = function() {
};
Foo.prototype.bar = 0;
var f1 = new Foo();
var f2 = new Foo();
console.log(f1.bar, f2.bar); // 0, 0 -- both are still using the `bar` on the prototype
++f1.bar;
console.log(f1.bar, f2.bar); // 1, 0 -- f1 now has its own
Foo.prototype.bar += 2;
console.log(f1.bar, f2.bar); // 1, 2 -- f2 is still using the `bar` on the prototype
The problem with this code is that since I'm declaring the countInstances after the constructor, I'm getting an error in the constructor code itself. How to correct this??
不,问题是您的实例没有 this.prototype
对象。函数上的 prototype
属性 不会复制为实例上的 prototype
属性;它被分配给他们作为他们的原型,这(有点令人困惑)不叫 prototype
。很长一段时间,它根本没有规范之外的名字。您可以通过 Object.getPrototypeOf(this)
或(从下一个规范开始,这将成为基于浏览器的 JavaScript 的标准)访问它 __proto__
属性.
但是放在原型上可能没有意义。我只是在函数本身上使用 属性:
var Component = function(name) {
this.name = name;
this.add = function(){Component.instances++;};
this.add();
};
Component.instances = 0;
但是你说你想统计构造函数创建的对象数量;上面计算了 add
方法被调用的次数。要计算构造函数创建的实例数,请在构造函数中递增它:
var Component = function(name) {
Component.instances++;
this.name = name;
this.add = function(){/*Presumably you're doing something here*/};
this.add();
};
Component.instances = 0;
我们将能够通过使用来做同样的事情:
function component() {
if(component.prototype.counter) { component.prototype.counter = 0; }
component.prototype.counter++;
this.add = function(){ /*... do something here....*/ }
}
通过在函数体内启动计数器,我们将能够保持计数(调用函数的次数)。
好的,所以我想在 javascript 中创建一个构造函数,它将计算使用此构造函数创建的实例总数。
var Component = function(name) {
this.name = name;
this.add = function(){this.prototype.countInstances++;};
this.add();
};
和
Component.prototype.countInstances=0;
据我理解正确,countInstances 变量被添加到原型中,它将作为所有实例的静态副本,并将作为我的计数器。
此代码的问题在于,由于我在构造函数之后声明了 countInstances,因此构造函数代码本身出现错误。如何纠正这个问题??
var ComponentCounter = 0;
var Component = function(name) {
this.name = name;
this.add = function(){this.prototype.countInstances++;};
this.add();
ComponentCounter++;
// or Component.counter++;
};
// or add it as a property of Component after it has been defined
// Component.counter = 0;
原型中的变量属于实例,因此您必须跟踪在实例之间持久保存的变量上的数据。
如果您希望 属性 附加到 class 本身,而不是 class 的实例,您不想添加 属性 原型:
var Component = function(name) {
this.name = name;
Component.instanceCount++;
};
Component.instanceCount = 0;
这样,您将每个名称分配给它的实例,并将实例总数分配给静态 class:
var foo = new Component('bar');
var baz = new Component('qux');
console.info(foo.name, baz.name, Component.instanceCount);
>> 'bar', 'qux', 2
As I understand it correctly, the countInstances variable is added to the prototype and will act like a static copy for all the instances and will act as my counter.
不,实际上它将是实例的 默认值,而不是 "static." 如果将它放在 Component.prototype
上,所有实例将通过原型链继承它,但通过实例更改它会给该实例它的 own 副本。示例:
var Foo = function() {
};
Foo.prototype.bar = 0;
var f1 = new Foo();
var f2 = new Foo();
console.log(f1.bar, f2.bar); // 0, 0 -- both are still using the `bar` on the prototype
++f1.bar;
console.log(f1.bar, f2.bar); // 1, 0 -- f1 now has its own
Foo.prototype.bar += 2;
console.log(f1.bar, f2.bar); // 1, 2 -- f2 is still using the `bar` on the prototype
The problem with this code is that since I'm declaring the countInstances after the constructor, I'm getting an error in the constructor code itself. How to correct this??
不,问题是您的实例没有 this.prototype
对象。函数上的 prototype
属性 不会复制为实例上的 prototype
属性;它被分配给他们作为他们的原型,这(有点令人困惑)不叫 prototype
。很长一段时间,它根本没有规范之外的名字。您可以通过 Object.getPrototypeOf(this)
或(从下一个规范开始,这将成为基于浏览器的 JavaScript 的标准)访问它 __proto__
属性.
但是放在原型上可能没有意义。我只是在函数本身上使用 属性:
var Component = function(name) {
this.name = name;
this.add = function(){Component.instances++;};
this.add();
};
Component.instances = 0;
但是你说你想统计构造函数创建的对象数量;上面计算了 add
方法被调用的次数。要计算构造函数创建的实例数,请在构造函数中递增它:
var Component = function(name) {
Component.instances++;
this.name = name;
this.add = function(){/*Presumably you're doing something here*/};
this.add();
};
Component.instances = 0;
我们将能够通过使用来做同样的事情:
function component() {
if(component.prototype.counter) { component.prototype.counter = 0; }
component.prototype.counter++;
this.add = function(){ /*... do something here....*/ }
}
通过在函数体内启动计数器,我们将能够保持计数(调用函数的次数)。