Sikau

Advanced JavaScript · Day 4 · about 25 min

this, call and bind

Who is "this", and how to choose.

this can change

In methods, this means the object the method was called on. But pass that method somewhere else — as a callback, say — and this can go missing. Today you'll learn the rules and how to control them.

this depends on the call

const shop = {
  name: "Momo House",
  hello() {
    return "Welcome to " + this?.name;
  },
};

console.log(shop.hello());
const loose = shop.hello;
console.log(loose());

Rule of thumb: look at what's left of the dot at the moment of the call. No dot, no this.

The rest of this lesson

  • Choosing this: call, apply, bind — and arrowsexplain
  • The four rules of thisexplain
  • Predict the outputpredict
  • What is this?match
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 27 lessons of Advanced JavaScript