All 28 lessons ▾

Inside JavaScript · Lesson 28 of 28

The this Keyword Part 2: Methods, call/apply/bind, Arrow Functions and the DOM

Updated 11 Nov 2026 · By Akshay, software engineer, India · 9 min read

So, same function, different this. Methods, call, apply, bind, arrow functions, and the DOM. So let's finish the this keyword, and the whole course!

The video for this lesson is coming soon.

A new lesson goes up every day at 11 am. Subscribe on YouTube so you don't miss it.

Got a doubt? Ask in the comments under the video.Subscribe

In one minute
  • Method call: this is the object before the dot
  • Taking a method out loses its this
  • call and apply run now; bind returns a copy
  • Arrow functions borrow this from where they're written
  • DOM handlers: this is the element

Part 1this inside a method

So this is the final episode of Inside JavaScript! In part one, we learned the rule. This depends on how a function is called. So today, we apply that rule everywhere. To methods, to call, apply, and bind, to arrow functions, and to the DOM. So let's start with methods.

method.js
1const movie = {2  title: "Inception",3  show: function () {4    console.log(this.title);5  },6};7 8movie.show();
Output
Inception

So when a function lives inside an object, we call it a method. Here, movie has a title, Inception, and a method, show, that logs this.title. Line eight calls movie.show. Now look at what's before the dot. movie! So inside show, this is movie. And this.title is Inception. That's the simplest way to remember it. For a method call, this is whatever is before the dot.

lost-this.js
1const show = movie.show;2 3show();4// TypeError: Cannot read properties of undefined

But now watch what happens when we take the method out. We store movie.show in a variable, show, and call it plainly. Now there's nothing before the dot. It's a plain call! So in strict mode, this is undefined, and reading title from undefined throws an error. The function is exactly the same. Only the call changed. So this is the most common this bug, and it happens all the time with callbacks, like passing a method to setTimeout.

REMEMBER THIS
Inamethodcall,thisis
theobjectbeforethe
dot.
take the method out, and you lose it

So remember this. In a method call, this is the object before the dot. movie.show, this is movie. But if you take the method out, and call it on its own, there's no dot, so there's no movie. The this is lost. So keep an eye on the dot, and you'll always know what this is.

Part 2call, apply and bind

So, can we choose this ourselves? Yes! Every function has three methods for exactly that. call, apply, and bind. Let's see.

call.js
1const movie2 = { title: "Interstellar" };2 3movie.show.call(movie2);4// Interstellar

So here's a second object, movie2, with its own title, Interstellar, but no show method. Can we reuse movie's show for it? Yes, with call. movie.show.call, movie2. call runs the function immediately, with this set to whatever you pass first. So this is movie2, and it prints Interstellar. So we borrowed a method, without copying it.

call-apply.js
1function greet(city, time) {2  console.log(this.title + " in " + city + " at " + time);3}4 5greet.call(movie, "Pune", "6 PM");6greet.apply(movie, ["Pune", "6 PM"]);7// Inception in Pune at 6 PM

And now if the function takes arguments? With call, you pass them one by one, after this. With apply, you pass them all together, in an array. That's the only difference between them. apply, array. So both run the function right away, and both print, Inception in Pune at six PM.

bind.js
1const showLater = movie.show.bind(movie);2 3setTimeout(showLater, 1000);4// after 1 second: Inception

And now bind is different. bind doesn't run the function. It returns a new copy of the function, with this locked forever. So showLater always has this as movie, no matter how it's called later. That fixes our bug from before. Pass showLater to setTimeout, and a second later, it prints Inception, not an error. So use bind when you hand a method to someone else, to call later.

REMEMBER THIS
callandapplyrunnow
withyourthis;bind
returnsacopywiththis
locked.
call: commas; apply: an array

So remember this. call and apply run the function right now, with the this you choose. call takes the arguments with commas, apply takes them in an array. bind doesn't run anything. It gives you a new function, with this locked in, to call later. So three tools, one job. Setting this.

Part 3Arrow functions

Now, arrow functions. They have a special relationship with this. In fact, they don't have a this of their own at all.

arrow-method.js
1const movie = {2  title: "Inception",3  show: () => {4    console.log(this.title);5  },6};7 8movie.show();   // undefined (in a script)

So arrow functions don't have their own this. Instead, they use the this of the place where they're written, their enclosing lexical context. So here, show is an arrow function, written inside an object literal. But an object literal isn't a function, so it doesn't create a this of its own. The enclosing this is the global one. And this.title gives undefined. So that's why you should never write methods as arrow functions.

arrow-inside.js
1const movie = {2  title: "Inception",3  show: function () {4    const later = () => {5      console.log(this.title);6    };7    setTimeout(later, 1000);8  },9};10 11movie.show();   // after 1 second: Inception

But now here's where arrow functions shine. show is a normal method, so when we call movie.show, its this is movie. Inside it, we create an arrow function, later. later has no this of its own, so it borrows show's this, which is movie. Even when setTimeout calls it a second later, this is still movie, and it prints Inception. Remember the trap with inner, from last episode? This is the fix. Arrow functions keep the this of the method around them. And now here's a simple trick for any arrow function. Imagine the arrow isn't there at all, and its code sits directly in the place around it. Whatever this would be there, that's this inside the arrow.

REMEMBER THIS
Arrowfunctionshaveno
ownthis.Theyusethe
thisofwherethey're
written.
great for callbacks inside methods, bad as methods

So remember this. An arrow function has no this of its own. It takes this from the place where it's written, lexically, just like scope. So arrow functions are perfect as callbacks inside methods, and a bad idea as methods themselves. And you can't change an arrow function's this with call, apply, or bind either. It stays locked to where it was written. Now read MDN's line again. Arrow functions don't provide their own this binding. They keep the this of the enclosing lexical context. Makes sense now.

Part 4this in the DOM

And finally, this in the browser's DOM, inside event handlers.

dom.js
1// <button id="likeBtn">Like</button>2 3btn.addEventListener("click", function () {4  console.log(this);   // <button id="likeBtn">5  this.textContent = "Liked!";6});

So when you attach a normal function as an event listener, this inside it is the element the listener is attached to. Here, the button. So this.textContent equals liked changes the button's own text. Very handy. And this.tagName gives you BUTTON. But be careful. If you use an arrow function here instead, this would come from outside, not the button. So in event handlers, use a normal function when you need this, or use event.currentTarget, which works with both.

this, in every situation
->
Global space: the global
object
->
Plain call: undefined (strict) or
window
->
Method call obj.f(): obj
->
call / apply / bind: whatever
you pass
->
Arrow function: this from
where it's written
->
DOM event handler: the
element
INTERVIEW ANSWER
thisissetbythecall:
theobjectbeforethe
dot,call/apply/bind,or
thearrow'ssurroundings.
the global object at the top;
undefined in a strict plain call

So let's put every case in one place. In the global space, this is the global object. In a plain call, it's undefined in strict mode, or window without it. Call a method, and this is whatever sits before the dot. With call, apply, or bind, it's whatever you pass. An arrow function takes this from where it's written. And in a DOM event handler, it's the element. Now this also has its own rules inside classes and constructors, with the new keyword. That's object-oriented JavaScript, a topic for another day.

Quick quiz
quiz.js
1const user = {2  title: "Asha",3  hi: function () {4    return this.title;5  },6  hey: () => this.title,7};8 9console.log(user.hi());10console.log(user.hey());11console.log(user.hi.call({ title: "Kabir" }));

What do the three lines print?

Show the answer

The first line calls user.hi. A method call, so this is user, and we get Asha. The second calls user.hey. But hey is an arrow function, so it ignores the dot, takes this from outside, the global object, and we get undefined. The third uses call, with an object whose title is Kabir. So this is that object, and we get Kabir. So Asha, undefined, Kabir. Did you get all three?

Part 5You finished the course!

So a quick recap of today, and then something special.

Everything in one minute
Method call: this is the object before the dot
Taking a method out loses its this
call and apply run now; bind returns a copy
Arrow functions borrow this from where they're
written
DOM handlers: this is the element
YOU DID IT!
Fromtheexecution
contexttoasyncawaitto
this:younowknowwhat
JavaScriptdoes,andwhy.
thank you for learning with me

And with that, you've finished Inside JavaScript! Now think about where we started. One execution context, two phases, and a call stack. Then hoisting, scope, closures, the event loop, callbacks, promises, async await, and now this. So you don't just know the syntax anymore. You know what JavaScript does behind the scenes, and why. That's what makes a great developer, and it's exactly what interviewers look for. So tell me in the comments which episode helped you the most. Thank you so much for learning with me.

Homework

In the comments, explain this in your own words, the way you'd explain it in an interview.

Show the answer

this is decided by how a function is called, not where it's written.

  • At the top level, it's the global object: window in the browser.
  • In a plain call, show(), it's undefined in strict mode, and the global object otherwise, because of this substitution.
  • In a method call, user.hi(), it's the object before the dot: user.
  • call and apply run the function with the this you pass. bind returns a copy with this fixed.
  • Arrow functions have no this of their own. They use the this of the code they're written in.
  • In a DOM event handler written as a normal function, it's the element the listener is on.

Give one small example of each, and you've answered it completely.

Try it first, then post yours in the comments under the video.

Your progress stays on this device.
A new lesson every day at 11 am

The video on YouTube, and the lesson as a swipe-through on Instagram.