3

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.

7
  • 1
    "Number of objects inside of a constructor" doesn't make a lot of sense. You can count the enumerable properties of an object (regardless of where the properties came from) with Object.keys(obj).length. Commented Apr 15, 2015 at 21:09
  • Uhm, you mean return 9, right ? Commented Apr 15, 2015 at 21:09
  • You don't have to prototype a count function. Is simple as EventData.length Commented Apr 15, 2015 at 21:11
  • So it would return 10, but why would it return 10 ? Commented Apr 15, 2015 at 21:17
  • I would need 10 if I was to include prototypes with the count. 8 would give me the number of parameters asked. Commented Apr 15, 2015 at 21:28

4 Answers 4

6

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;
};
Sign up to request clarification or add additional context in comments.

4 Comments

I was just about to save my answer exactly as yours, so i've deleted it and upvoted since its a pretty good approach :)
@dfsq i think he Op means the properties present, not the arguments passed. arguments.length will give 3 if we pass only 3 arguments
@A.B Hm.. originally it was parameters, I guess. Looks like you are right.
still needs confirmation @dfsq may b this is the scenario, +1 fro edit
4

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

Comments

3

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.