Methods
Class methods define behavior shared by instances.
class User {
constructor(name) {
this.name = name;
}
greet() {
return `Hello, ${this.name}`;
}
}
const user = new User("Alice");
console.log(user.greet()); // Hello, Alicegreet is a method.
It can read instance data through this.
Instance Methods
Methods written inside a class body are instance methods.
class Counter {
constructor() {
this.count = 0;
}
increment() {
this.count += 1;
return this.count;
}
reset() {
this.count = 0;
}
}
const counter = new Counter();
console.log(counter.increment()); // 1
console.log(counter.increment()); // 2
counter.reset();
console.log(counter.count); // 0You call instance methods on an instance.
counter.increment();Methods Use this
Inside a method, this usually refers to the instance before the dot.
const counter = new Counter();
counter.increment();Inside increment, this is counter.
That is why this works:
this.count += 1;Methods Can Receive Parameters
class Cart {
constructor() {
this.items = [];
}
addItem(name, price) {
this.items.push({ name, price });
}
getTotal() {
return this.items.reduce((total, item) => total + item.price, 0);
}
}
const cart = new Cart();
cart.addItem("Keyboard", 1200);
cart.addItem("Mouse", 700);
console.log(cart.getTotal()); // 1900Methods are regular functions with special method syntax.
They can take parameters, return values, and call other methods.
Calling One Method From Another
Use this.methodName().
class User {
constructor(name) {
this.name = name;
}
getDisplayName() {
return this.name.toUpperCase();
}
greet() {
return `Hello, ${this.getDisplayName()}`;
}
}
const user = new User("Alice");
console.log(user.greet()); // Hello, ALICEthis.getDisplayName() calls another method on the same instance.
Method Extraction Pitfall
Class methods can lose this when extracted.
class User {
constructor(name) {
this.name = name;
}
greet() {
console.log(`Hello, ${this.name}`);
}
}
const user = new User("Alice");
const greet = user.greet;
greet(); // TypeError in strict modeThe method was separated from the instance.
The call is now:
greet()not:
user.greet()So this is lost.
Fixing Lost Method Context
Use a wrapper:
setTimeout(() => {
user.greet();
}, 1000);Or use bind:
const greet = user.greet.bind(user);
greet(); // Hello, AliceThis connects back to the this module.
Methods Are Shared
Methods declared in the class body are shared by instances through the prototype.
class User {
greet() {
return "Hello";
}
}
const a = new User();
const b = new User();
console.log(a.greet === b.greet); // trueBoth instances use the same method function.
This is memory-efficient.
You will learn prototypes in the next module.
Avoid Defining Methods Inside the Constructor
This works:
class User {
constructor(name) {
this.name = name;
this.greet = function () {
return `Hello, ${this.name}`;
};
}
}But it creates a new function for every instance.
Prefer class methods:
class User {
constructor(name) {
this.name = name;
}
greet() {
return `Hello, ${this.name}`;
}
}Method Return Values
Methods can return values.
class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
}
getArea() {
return this.width * this.height;
}
}
const rectangle = new Rectangle(10, 5);
console.log(rectangle.getArea()); // 50Methods can also mutate instance state.
class Toggle {
constructor() {
this.on = false;
}
toggle() {
this.on = !this.on;
}
}Be clear about whether a method returns data, changes state, or both.
Best Practices
Use class methods for shared behavior.
Use this to access instance data.
Keep methods focused on one task.
Prefer returning values when possible.
Be careful when passing methods as callbacks.
Avoid defining normal methods inside the constructor unless you have a specific reason.
Common Mistakes
Mistake 1: Forgetting this
class User {
constructor(name) {
this.name = name;
}
greet() {
return `Hello, ${name}`;
}
}name is not a local variable in greet.
Correct:
return `Hello, ${this.name}`;Mistake 2: Extracting a Method and Losing this
const greet = user.greet;
greet();Use:
const greet = user.greet.bind(user);or:
() => user.greet()Mistake 3: Making Methods Too Large
A method that validates input, fetches data, formats output, updates state, and renders UI is doing too much.
Split large behavior into smaller methods.
Quick Check
What does this log?
class Counter {
constructor() {
this.count = 0;
}
increment() {
this.count += 1;
return this.count;
}
}
const counter = new Counter();
console.log(counter.increment());
console.log(counter.increment());It logs:
1
2Summary
Class methods define behavior for instances.
- Instance methods are called on objects created from the class.
- Methods use
thisto access instance data. - Methods can receive parameters and return values.
- Methods can call other methods with
this.methodName(). - Class methods are shared between instances.
- Extracted methods can lose
this. - Keep methods focused and readable.