Why does the constructor method have 2 parameters but have 3 properties?

In this lesson, the Cat class constructor method has 2 parameters which are 2 out of its 3 properties namely - name and usesLitter But has 3 properties in whole.

Why is this so? Why is the third property (behaviour) not included as the third parameter?

If we look at the code, we see that there’s a method for affecting the behavior (incrementBehavior()). Since it always starts at 0, we don’t need to tell it this every time we instantiate a new cat class.

For example, when an animal is born in real life, their age would start at 0. The way to describe that in a class is similar to this example, because nothing is ever born at an age of anything other than 0.

class Cat {
  constructor(name, usesLitter) {
    this._name = name;
    this._usesLitter = usesLitter;
    this._behavior = 0;
  }

  get name() {
    return this._name;
  }

  get usesLitter() {
    return this._usesLitter;
  }

  get behavior() {
    return this._behavior;
  }  

  incrementBehavior() {
    this._behavior++;
  }
}
1 Like