简体   繁体   English

如何计算构造函数中对象的数量javascript

[英]How to count the number of objects in a constructor javascript

Im working with javascript and would like to know the number of objects inside of a constructor.我正在使用 javascript 并想知道构造函数中的对象数量。

I have this:我有这个:

var EventData = function(ID, Name, StartDate, StartTime, EndDate, EndTime, Location, Notes){
  this.type = "event";
  this.id = ID;
  this.name = Name;
  this.startDate = StartDate;
  this.startTime = StartTime;
  this.endDate = EndDate;
  this.endTime = EndTime;
  this.location = Location;
  this.notes = Notes;
};
EventData.prototype.count = function(){
  return //some code;
};

And i want to call something like this:我想这样称呼:

var Start = function(){
  var thisEventData = new EventData(1,"Bill", 1,1,1,1, "Home", "N/A");
  console.log(thisEventData.count());
};

Where thisEventData.count() would return 10. thisEventData.count() 将返回 10。

The most natural way in this case would be to store parameters count in some property (maybe private).在这种情况下,最自然的方法是将参数计数存储在某些属性中(可能是私有的)。 I'm not sure why you need this, but, yea, something like this:我不确定你为什么需要这个,但是,是的,像这样:

var EventData = function (ID, Name, StartDate, StartTime, EndDate, EndTime, Location, Notes) {
    this._paramsCount = arguments.length;
    this.type = "event";
    this.id = ID;
    this.name = Name;
    this.startDate = StartDate;
    this.startTime = StartTime;
    this.endDate = EndDate;
    this.endTime = EndTime;
    this.location = Location;
    this.notes = Notes;
};
EventData.prototype.count = function () {
    return this._paramsCount;
};

UPD.更新。 Based on the edited question, look like you want to calculate number of own properties of the instance.根据编辑过的问题,您似乎想要计算实例的自身属性数量。 In this case you would use convenient Object.keys method which returns an array of property names.在这种情况下,您将使用方便的Object.keys方法,该方法返回一个属性名称数组。

EventData.prototype.count = function () {
    return Object.keys(this).length;
};

Just get the properties of the EventData object as an array with Object.keys , and then the length of the array?只需使用Object.keysEventData对象的属性作为数组获取,然后获取数组的长度?

EventData.prototype.count = function(){
  return Object.keys(this).length;
};

If you want to know the number of canonical parameters, use the Function.length property, in your case:如果您想知道规范参数的数量,请在您的情况下使用Function.length属性:

EventData.length

or或者

thisEventData.constructor.length

Note that it won't count additional parameters (variable arguments) since it infers the count from the constructor object.请注意,它不会计算其他参数(变量参数),因为它会从构造函数对象推断计数。

If you want to know the property count of the constructed object, use Object.keys :如果您想知道构造对象的属性计数,请使用Object.keys

Object.keys(thisEventData).length

This will give you the count of the own properties (excluding the prototype chain).这将为您提供自己属性的计数(不包括原型链)。

Or just write your own customizable logic if you need something else, for instance if you want non-falsey objects:或者,如果您需要其他东西,只需编写自己的可自定义逻辑,例如,如果您想要非虚假对象:

function getPropertyCount(obj, withPrototypeChain) {
    var prop;
    var count = 0;

    for (prop in obj) {
        if ((withPrototypeChain || obj.hasOwnProperty(prop)) && obj[prop])
            ++count;
    }

    return count;
}

To build a bit on the accepted answer:在接受的答案上建立一点:

export class Person {
      constructor (name, age) {
        if (arguments.length !== 2) {
          throw new Error('invalid Person argument count')
        }
        this.name = name
        this.age = age 
      }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM