An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
Difference between call,apply and bind
In Object Oriented JavaScript, everything is an object and therefore we can set and access additional properties to functions and methods via the prototype
Call( ): The call() method invokes a function with a given 'this' value and arguments provided one by one. This means that we can call any function, and explicitly specify what 'this' should reference within the calling function.
Apply( ): Invokes the function and allows you to pass in arguments as an array.
Bind(): returns a new function, allowing you to pass in an array and any number of arguments.
When we use the bind() method:
1.The JS engine is creating a new invite instance and binding friend1 and friend2 as its 'this' variable. So basically it copies the invite function.
2.After creating a copy of the invite function it is able to call inviteFriend1( ) and inviteFriend2( ), although it wasnt on the friend1 and friend2 object initially. It will now recognizes its properties and its methods.
Call and apply are pretty interchangeable. Both execute the current function immediately. You need to decide whether its easier to send in an array or a comma separated list of arguments. Whereas Bind creates a new function that will have this set to the first parameter passed to bind().
Thank You!
Original Link: https://dev.to/heba_shakeel/difference-between-call-apply-and-bind-4p98
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To