Methods & this in JavaScript
Methods are functions stored as properties, with this bound to the receiver at call time: user.greet() runs greet with this === user. Shorthand syntax (greet() {...}) defines them cleanly, but this is dynamic — detached references (const g = user.greet; g()) lose the receiver and see undefined (strict) or global (sloppy).
Arrow functions as methods inherit lexical this instead — usually wrong for object methods needing their object. The rule: shorthand methods for object behavior, arrows for callbacks closing over outer this.
Receiver binds this
obj.method() sets this to obj for that call — the same function behaves per receiver.
Detachment loses it
const g = obj.method; g() calls with undefined/global this — the #1 this bug, fixed by binding or arrows at definition.
Shorthand syntax
{ name, greet() { return "hi " + this.name; } } — concise methods with proper dynamic this.
Arrows differ
Arrow methods capture enclosing this — right for callbacks inside methods, wrong as the methods themselves.
let user = { name: "Ada", greet() { return "hi " + this.name; }};console.log(user.greet());Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.