Back to Browse

call and apply in JavaScript | call() and apply() methods in JavaScript

1.0K views
Jul 25, 2022
6:57

In JavaScript call() and apply() methods are used to manually pass an object as a value of 'this' inside a function. Let us say, you have a function function Animal(name, age){ this.name = name; this.age = age; } and an object var Dog = { color : "black" } you can pass Dog as the value of this inside Animal by either using call() or apply() methods as shown below: Animal.call(Dog,"foo",20) or Animal.apply(Dog, ["foo",30]) calling a function using apply() and call() is called Indirect Invocation Pattern. This is a very popular JavaScript interview question, and I hope now you should be able to answer this question confidently

Download

0 formats

No download links available.

call and apply in JavaScript | call() and apply() methods in JavaScript | NatokHD