bind in javascript 1

bind in javascript

bind() returns a bound function that, when executed later, will have the correct context ("this") for calling the original function.

Here is what the above code is Doing:
1. We create a function called ‘bind’ that takes a function and a context as arguments.
2. We return a function that calls the original function, but with the context set to the value passed in.
3. We create a function called ‘sayName’ that logs the name property of the context it is called with.
4. We create an object called ‘user’ with a name property.
5. We bind the ‘sayName’ function to the ‘user’ object.
6. We call the bound function.

Similar Posts