Sign in
Log inSign up

Function Constructor return value confusion "with and without prototype" !!

Default profile photo
Anonymous
·May 4, 2018
function Counter() {
  this.sum = 0;
  this.count = 0;
}
Counter.prototype.add = function(array) {
  array.forEach(function(entry) {
    this.sum += entry;
    ++this.count;
  }, this);
  // ^---- Note
};

const obj = new Counter();

When I run obj in the console then In the output I am getting add function ,not the object with sum and count properties but when I remove the prototype from counter then I am getting object with property sum and count.

I am confused that why I am not getting object with property sum and count apart from add function 
When I am adding prototype to function constructor Counter.