简体   繁体   中英

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.

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.

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.

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?

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:

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(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 
      }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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