The this Keyword
IntermediateThe four rules that decide what this points to in a call (default, implicit, explicit and new), and why a method loses its object when you pass it around.Builds on Execution Context, Primitive vs Reference Types
Empty
Empty
Empty
Empty
Spaceplay or pause←→previous or next stepRback to the start
Explanation
2 min read
On this page
What is the this keyword in JavaScript?
this is a value every regular function call receives, next to its arguments. Unlike variables,
which are found through the scope where the code was written, this is decided by how the
function is called. The same function can see a different this on every call.
Arrow functions are the exception: they have no this of their own and use the this of the code
around them.
How does JavaScript decide what this is?
Look at the call and check four rules, strongest first:
new:new User()creates an empty object, andthisis that object.- Explicit:
fn.call(obj),fn.apply(obj)andfn.bind(obj)setthistoobj. A function made withbindkeeps thatthisfor good; onlynewcan override it. - Implicit:
obj.fn()setsthisto the object right before the dot. - Default: a plain call
fn()getsundefinedin strict mode, modules and classes. In old sloppy-mode scripts it gets the global object (window) instead.
Event listeners add one more case: in element.addEventListener('click', fn), a regular fn gets
the element as this.
What mistakes do people make with this?
Mini example
function whoAmI() { return this.id;} const a = { id: 'a', whoAmI };const b = { id: 'b' };const bound = whoAmI.bind(b); console.log(a.whoAmI()); // 'a': implicitconsole.log(a.whoAmI.call(b)); // 'b': explicit beats implicitconsole.log(bound.call(a)); // 'b': bind is permanentQuick check
In the mini example, what does bound.call(a) return?
Further reading
All steps as text
- Step 1:
The script runs in strict mode, as modules and classes always do. The global context starts.
- Step 2:
Creation phase:
greetandUserare stored as functions, anduser,adminandlooseare reserved.Lines 3–18
- Step 3:
Two objects are created.
usergets agreetproperty pointing to the same function;adminhas none.Lines 7–8
- Step 4:
user.greetlooks upgreeton the user object and finds it there.Line 10
- Step 5:
Rule 1, implicit binding: the function is called through
user., sothisis the object before the dot.Line 10
- Step 6:
this.namereads name from the user object.Line 4
- Step 7:
greet returns, and Hi, Ali is printed.
Lines 4–10
- Step 8:
Rule 2, explicit binding:
callruns the same function withthisset to the object you pass, hereadmin.Line 11
- Step 9:
this.namenow reads from admin, so Hi, Sara is printed. Same function, differentthis.Lines 4–11
- Step 10:
Rule 3,
new: it creates an empty object and callsUserwiththisset to that new object.Line 16
- Step 11:
this.name = namewrites onto the new object.Line 14
- Step 12:
newreturns the new object, and.namereads Omar from it.Line 16
- Step 13:
The line is done, and nothing holds on to the object, so it is unreachable and can be freed.
Line 16
- Step 14:
loosecopies the function, not the object. The link touseris gone.Line 18
- Step 15:
Rule 4, default binding:
loose()has no object before the dot, so in strict modethisis undefined.Line 19
- Step 16:
Reading
namefrom undefined throws a TypeError, and the uncaught error ends the script.Line 4
- Step 17:
thisis decided by how a function is called, not where it is written. Check the call:new, then call/apply/bind, then an object before the dot, else undefined.