DEV Community

Cover image for The Magic of this, call(), apply(), and bind() in JavaScript
Bhupesh Chandra Joshi
Bhupesh Chandra Joshi

Posted on

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

What this means in JavaScript
You know that rumours are the mistruths which spread early and this keyword refers to the function it appears ,it is mistaken, The value of this determined dynamically(how it is invoked) , not on the function definition. If you want to understand this well, you

The prototype mechanism is a look-up chain for properties and inheritance and this is related to prototype mechanism. The concept of fake classes arrived from the this mechanism, classes mimic the objects / functions. if you can create a object of a function,

Authers said the desire to bring class and inheritance design pattern thinking to JavaScript is just about the worst thing you could try to do. The syntax can do a lot with you, you wonder that classes are present,classes are based on prototypal inheritance,it is considered as you are implementing the inheritance.

You know about the behavior delegation. This is more than syntactic preference. Delegation is an entirely different, and more powerful, design pattern, one that replaces the need to design with classes and inheritance. I will explain what this means, it adds the prototype property on the object and instead of copying ,it leverages the principle of link, if you add a new property to the prototype, you can access it later.Learn and embrace how the object prototype system actually works. You need to understand the thinking of javaScript, the recommended inheritance is javaScript is prototype chaining for performance.

Performance Considerations
1-Memory Efficiency
2-Property Lookup Time
3-Optimization by Engines

this inside normal functions
JS Rule: We leverage this as window (global) object in normal function or undefined in Strict mode.

This doesn't have any home,it can be anywhere.

This inside object:
When a function inside a object,in such case this points to that object.

abhishek.speak(), means this is Abhishek this time. Let's consider, Abhishek,a groom in movie and he speaks i am looking handsome,
So this is Abhishek and he is talking about himself.

What call() does
It depicts which actor will deliver the dialogues. We know about code borrowing and call method is just like object borrowing. It's method borrowing on inheritance,you must understand this by this image.

What apply() does
Apply() method leveraged for the changing the object associated with this.

What bind() does
bind freezes the this to perticular object. So, you can't able to change the value of this by apply.

Top comments (0)