Sub topic 3
Adding Behavior with Instance Methods
This chapter assumes you can declare a class and write a constructor that sets properties on this. It also assumes you know how template literals work and how to call a method on an object.
An instance method is a function defined in the class body. You call it on an instance, and inside it this refers to that instance. Methods are what turn a class from a bag of data into an object with behavior.
Defining and Calling Methods
Inside a class body, you write a method as a name, a parameter list, and a body. You don't use the function keyword, and you don't put commas between members.
class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
}
area() {
return this.width * this.height;
}
perimeter() {
return 2 * (this.width + this.height);
}
describe() {
return `${this.width}x${this.height}, area ${this.area()}`;
}
}
const r = new Rectangle(2, 3);
console.log(r.area()); // 6
console.log(r.perimeter()); // 10
console.log(r.describe()); // '2x3, area 6'Notice that describe() calls this.area(). One method reaches another the same way it reaches data: through this.
Methods That Change State
Methods can also update the instance. If a method returns this, callers can chain several calls together:
class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
}
scale(factor) {
this.width *= factor;
this.height *= factor;
return this;
}
area() {
return this.width * this.height;
}
}
const r = new Rectangle(2, 3);
console.log(r.scale(2).area()); // 24
console.log(r.width); // 4Return this only from methods that modify the object. If a method computes a value, return the value.
One Method, Many Instances
Every instance created from the class shares the same method. Each instance holds its own data, but the method function is not copied onto each object. That is why methods don't show up when you list an instance's own keys:
class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
}
area() {
return this.width * this.height;
}
}
const a = new Rectangle(1, 2);
const b = new Rectangle(5, 5);
console.log(Object.keys(a)); // [ 'width', 'height' ]
console.log(a.area === b.area); // trueThe next chapter explains where shared methods actually live.
Common Errors
| Mistake | Result |
|---|---|
| Writing `width * height` instead of `this.width * this.height` | `ReferenceError: width is not defined` |
| Putting commas between methods, as in an object literal | `SyntaxError` |
| Writing `function area() {}` inside the class body | `SyntaxError` |
| Calling `r.area` without parentheses | You get the function, not the number |
Exercise
Add a method isSquare() to Rectangle that returns true when width equals height. What does new Rectangle(2, 3).scale(2).isSquare() return?
Show answer
Define isSquare() { return this.width === this.height; }. The call returns false, because scaling 2x3 by 2 gives 4x6 and the sides are still unequal.
Exercise
A teammate writes class Counter { count = 0, increment() { this.count++; } } and gets a SyntaxError. Why?
Show answer
Members in a class body are not separated by commas. Remove the comma after count = 0 (a semicolon or a newline is fine).