Why
this.fire = function(...)and not an arrow function? Here's the full breakdown.
JavaScript offers multiple ways to define functions:
function greet() {}const greet = function() {}const greet = () => {}While arrow functions offer brevity and clarity, they do not have their own this , which is why choosing between the two depends entirely on your use case.
this BindingFunction expressions define their own this context depending on how they're called:
const obj = { count: 0, increment: function () { console.log(this); // refers to `obj` }, };
Arrow functions lexically bind this , meaning they inherit this from the surrounding context:
const obj = { count: 0, increment: () => { console.log(this); // refers to global object (or undefined in strict mode) }, };
this.fire = function (...)?Because this refers to the object instance (Move in this case), and the fire method needs to explicitly bind thisObj or use the current instance as context.
If you used an arrow function, it would ignore thisObj and this would not refer to the Move instance or a provided scope β breaking functionality like .call():
this.fire = (o, thisObj) => { const scope = thisObj || window; // `this` here is lexically bound and can't be overridden with `.call()` this.handlers.forEach((item) => item.call(scope, o)); // β unpredictable };
| Use Case | Prefer Function | Prefer Arrow Function |
|---|---|---|
Needs dynamic thisbinding | β Yes | β No |
| Callback inside method | β Yes | β
Yes (if no thisdependency) |
| Inside classes or object methods | β Yes | β No (unless explicitly static) |
| Event handlers | β Usually | β Only if you bind explicitly |
| Simple one-liners / pure funcs | β Overhead | β Perfect match |
const numbers = [1, 2, 3]; const squares = numbers.map(n => n * n);
class Timer { constructor() { this.seconds = 0; setInterval(function () { this.seconds++; // `this` needs to refer to Timer β so we bind }.bind(this), 1000); } }
const counter = { value: 0, increment: () => { this.value++; // β `this` is not `counter` }, };
this?A: Because arrow functions are designed for lexical scoping. They inherit this from the context in which they are defined, rather than from how they are called.
A: When the method depends on this referring to the class instance. Arrow functions donβt have their own this, so they wonβt work properly in instance methods.
A: No. If the function needs a dynamic or context-based this, you must use a traditional function.
.call() or .apply() behave with arrow functions?A: It has no effect. You cannot change the this value of an arrow function with .call() or .apply().
Use function declarations or expressions when you rely on dynamic this, such as in methods or callbacks that depend on object context. Use arrow functions for everything else β especially in callbacks, map/reduce logic, and stateless utilities.
Test your understanding with 3 quick questions