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
On this page of the site you can watch the video online call and apply in JavaScript | call() and apply() methods in JavaScript with a duration of hours minute second in good quality, which was uploaded by the user NomadCoder 24 July 2022, share the link with friends and acquaintances, this video has already been watched 943 times on youtube and it was liked by 42 viewers. Enjoy your viewing!