Learn JavaScript
❯ objects.methods_this

Methods & this in JavaScript

$concept

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.

$how it works
1

Receiver binds this

obj.method() sets this to obj for that call — the same function behaves per receiver.

2

Detachment loses it

const g = obj.method; g() calls with undefined/global this — the #1 this bug, fixed by binding or arrows at definition.

3

Shorthand syntax

{ name, greet() { return "hi " + this.name; } } — concise methods with proper dynamic this.

4

Arrows differ

Arrow methods capture enclosing this — right for callbacks inside methods, wrong as the methods themselves.

$example
let user = {
name: "Ada",
greet() { return "hi " + this.name; }
};
console.log(user.greet());
Output
hi Ada
1greet is defined with shorthand method syntax inside the literal.
2Called as user.greet(), this binds to user — this.name reads Ada.
3Detached (const g = user.greet), the same call would lose its receiver.
$use cases
Object behavior with shorthand methods and dynamic this
Binding handlers (bind) or arrow-wrapping to preserve receivers
Avoiding arrow functions as object methods
Reading detached-method bugs as lost-receiver bugs

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.