What’s the difference between .call and .apply?

What’s the difference between .call and .apply?

Call( ) and Apply( ) in JS is most commonly used to invoke a method or used in constructors & classes for Inheritance purposes.

The first parameter to be passed in these two methods is an Object (current reference/current context). The difference comes in the concluding parameters.

Apply( ) takes Array as it’s next parameter. The array represents the arguments for the target method.

Call( ) takes arguments in comma separated format.

Usage:
Use apply ( ), if you don’t know the number of arguments you will be passing, or if they are already in an array.
With call ( ), you can write a method once and then inherit it in another object, without having to rewrite the method for the new object.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>