Stage 2 · Lesson 5 of 10
How this works
The gets its value from how a function is called, not where it is written. Called as obj.method(), this is obj. Called as a plain function, it is undefined in strict mode and in modules.
have no this of their own. They use the one from the surrounding code, which makes them handy for inside methods. Passing a method as a callback, such as setTimeout(user.greet, 100), loses its this.
Calling bind, or wrapping the call in an arrow function, fixes that. You will meet this in classes, in (where it is the element) and in older library code. Modern framework code uses it less, so the goal is to predict it, not to design around it.
A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock
Resources for this stage
Start with· follow one of these
- CoursePaidGrokking JavaScript Fundamentals
Design Gurus · Beginner · $50
Guided beginner course on the language core, with quizzes after every chapter
- GuideFreeJavaScript.info
Ilya Kantor
Free and thorough: Part 1 for the language, Part 2 for the DOM and events, Part 3 for fetch
Practise
Keep handy
Also worth your time
- You Don't Know JS YetGo deeper
For when scope, this, coercion or async behaviour still feel like magic