Javascript Call and Apply functions1 min read


What are Call and Apply functions?

Call and Apply functions are predefined Javascript functions which are used to bind object passed into function.

In other words with Call or Apply we invoke a function with an owner object as the first parameter.



let car = {
model: 'BMW',
year: '2009'
let car2 = {
model: 'MERCEDES',
year: '2017'
function printCarProps(msg) {
console.log(`${msg}` ${this.model} ${this.year});
},"Best car is:"); // outputs: Best car is BMW 2009,"Only"); // outputs: Only MERCEDES 2017

Apply function does the same thing as Call but the only difference is the way in which each of these functions accepts arguments.

Apply accepts it as an array:



Call accepts it as subsequent parameters:



Call and Apply both are used to force the this value when a function is executed but the difference is in passing arguments.

(Visited 30 times, 1 visits today)

Leave a comment

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