🟒 Easy'this' KeywordπŸ“– Theory Question

How does 'this' work in different contexts?

πŸ’‘

Hint

Determined by how a function is called, not where it is defined

Full Answer

this is determined by how a function is called:

  • Global β†’ window / undefined (strict)
  • Method call β†’ the object before the dot
  • new β†’ the newly created object
  • call/apply/bind β†’ whatever you pass
  • Arrow function β†’ outer lexical scope
  • Event listener β†’ the element that fired
const obj = { val: 42, getVal() { return this.val; } };
const fn = obj.getVal;
fn();          // undefined β€” lost context!
fn.call(obj);  // 42 β€” restored

More 'this' Keyword Questions

🟒 EasyExplain the four rules of this binding: default, implicit, explicit, and new.β†’

Practice this in a timed sprint β†’

5 free questions, no signup required

⚑ Start Sprint