Skip to main content

When to use a method vs a handler in Go

A Method is a skill an object can do, while a Handler is the specific reaction that triggers that skill when an event occurs.

Method ===> Focuses on the "what" (the skill)​

const chef = {
// This is a METHOD
chop(ingredient) {
console.log(`Chef is skillfully dicing the ${ingredient}.`);
}
};

Handler ===> Focuses on the "when" (the event)​

// This is a HANDLER
function onNewOrderReceived(event) {
const ingredient = event.orderItem; // e.g., "Onion"

// The handler triggers the method
chef.chop(ingredient);

console.log("Order handled!");
}

// Registering the handler to the "Order System"
orderSystem.addEventListener("ticket_arrived", onNewOrderReceived);