Photo by Jongsun Lee on Unsplash
Understand Javascript's Call, Bind, and Apply in the Simplest and Easy Way Possible!!๐ฅ
Table of contents
No headings in the article.
We all know how much these Javascript methods are confusing to understand. But there is always a simple way to explain complex things. Close your eyes and imagine yourself in the vast universe! You have an amazing spaceship and you are on a mission to explore different planets in the universe.
On each planet, you have to collect energy crystals to power your spaceship, as we play in the game 'No Mans Sky'!! The energy crystals are in different shapes and sizes, and you have to add their values to see how much energy you have collected.
Let's start with Call:
"Call" is like giving a direct command to your spaceship computer to perform a specific task. In our case, you want to ask the computer to add the values of the energy crystals you have collected.
Here's an example of what that would look like in code:
With "Call," you can ask the computer to add the values of the energy crystals in a straightforward and direct way. The computer will instantly perform the task and give you the result.
Now let's move on to Bind:
"Bind" is like telling your spaceship computer ahead of time that you want to add the values of energy crystals, but also giving it one of the values.
Here's an example of what that would look like in code:
With "Bind," you can tell the computer ahead of time that you always want to add a specific value to the energy crystals, and the computer will remember that. The next time you want to add values, you can just give it the second value, and the computer will do the rest.
Finally, let's talk about Apply:
"Apply" is like giving your spaceship computer a group of values to add together, instead of giving them one by one.
Here's an example of what that would look like in code:
With "Apply," you can give the computer all the values of energy crystals at once, and the computer will add them up quickly and efficiently. This is a great option when you have a lot of values to add.
So, there you have it! With Call, Bind, and Apply, you can ask your spaceship computer to add the values of the energy crystals in different ways and complete your mission to explore the universe with ease!
Hope this was helpfull!!๐๐