Menu

Post image 1
Post image 2
1 / 2
0

The Magic of `this`, `call()`, `apply()`, and `bind()` in JavaScript

DEV Community·Pratham·23 days ago
#6SeQR8mG
#pattern#part#syntax#example#fullscreen#call
Reading 0:00
15s threshold

How to take control of this — and borrow functions like a pro. In a previous article, I covered how this works in JavaScript — it refers to the object that is calling the function. Simple rule: look left of the dot. But here's the thing that bugged me: what if I want to control what this refers to? What if I have a function that belongs to one object, but I want to use it with a different object? Am I supposed to copy-paste the function? Create a duplicate? Nope. JavaScript gives you three methods to explicitly set this : call() , apply() , and bind() . They're like a remote control for this — you decide exactly what it points to. These three methods were one of those topics in the ChaiCode Web Dev Cohort 2026 that seemed intimidating at first but turned out to be beautifully simple. Let me show you. Quick Refresher: What this Means Before we dive into call , apply , and bind , let's make sure the foundation is solid. this refers to the object that is calling the function.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More