【问题标题】:Javascript constructor function to count the number of instancesJavascript构造函数来计算实例数
【发布时间】:2015-06-05 06:31:47
【问题描述】:

好的,所以我想在 javascript 中创建一个构造函数,它将计算使用此构造函数创建的实例的总数。

var Component = function(name) {
    this.name = name;
    this.add = function(){this.prototype.countInstances++;};
    this.add();
};

Component.prototype.countInstances=0;

按照我的正确理解,countInstances 变量被添加到原型中,它将充当所有实例的静态副本,并将充当我的计数器。

此代码的问题在于,由于我在构造函数之后声明了 countInstances,因此我在构造函数代码本身中遇到了错误。如何纠正这个问题??

【问题讨论】:

  • 你为什么不在原型上声明函数呢?按照您现在的方式,add 将为每个实例重新创建。

标签: javascript object constructor count


【解决方案1】:

如果您希望将属性附加到类本身,而不是类的实例,则不希望将该属性添加到原型:

var Component = function(name) {
  this.name = name;
  Component.instanceCount++;
};

Component.instanceCount = 0;

这样,您将每个名称分配给它的实例,并将总实例计数分配给静态类:

var foo = new Component('bar');
var baz = new Component('qux');

console.info(foo.name, baz.name, Component.instanceCount);

>> 'bar', 'qux', 2

【讨论】:

    【解决方案2】:

    按照我的正确理解,countInstances 变量被添加到原型中,它将充当所有实例的静态副本,并将充当我的计数器。

    不,它实际上是实例的默认值,而不是“静态”。如果你把它放在Component.prototype 上,所有的实例都会通过原型链继承它,但是通过一个实例改变它会给那个实例自己的它的副本。示例:

    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
    

    此代码的问题在于,由于我在构造函数之后声明了 countInstances,因此我在构造函数代码本身中遇到了错误。如何纠正这个问题??

    不,问题是您的实例没有this.prototype 对象。函数上的prototype 属性不会复制为实例上的prototype 属性;它被分配给他们作为他们的原型,(有点令人困惑)不称为prototype。很长一段时间以来,它根本没有超出规范的名称。您可以通过Object.getPrototypeOf(this)__proto__ 属性(这将是下一个规范中基于浏览器的 JavaScript 的标准)访问它。

    但把它放在原型上可能没有意义。我只是在函数本身上使用一个属性:

    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;
    

    【讨论】:

      【解决方案3】:
      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;
      

      原型中的变量属于实例,因此您必须跟踪在实例之间持久存在的变量上的数据。

      【讨论】:

        【解决方案4】:

        我们也可以使用:

        function component() {
            if(component.prototype.counter) {    component.prototype.counter = 0; }
            component.prototype.counter++;
            this.add = function(){ /*... do something here....*/ }
        }
        

        通过在函数体内启动计数器,我们将能够保持计数(函数调用的次数)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-10-01
          • 1970-01-01
          • 2011-04-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-05
          相关资源
          最近更新 更多