Difference between .call and .apply
Basically, they differ on how they handle function arguments . Both functions allow you to control the meaning of 'this' inside the called function, but the difference is in how arguments are passed to the function. The apply() method is identical to call() , except apply() requires an array as the second parameter. The array represents the arguments for the target method.
It calls a function with given values and arguments provided individually
It calls a function with given values and arguments provided as an array or array object.
The first argument, in both cases, is the object reference that will represent 'this' inside the called function. So that's the difference between call and apply. Both can be called on functions, which they run in the context of the first argument. In call the subsequent arguments are passed in to the function as they are, while apply expects the second argument to be an array that it unpacks as arguments for the called function. Let's see an example which will make it clearer.
- What is noscript tag?
- Difference between '=', '==' and '===' operators?
- What is Browser Object Model
- How to test a string as a literal and as an object ?
- What is Associative Array? How do we use it?
- What's the Difference Between Class and Prototypal Inheritance?
- Event bubbling and Event Capturing in JavScript?
- What is an immediately-invoked function expression?
- What Are RESTful (REpresentational State Transfer)Web Services?