Sivasankar Saravanan
Sivasankar Saravanan

Reputation: 45

Difference between fn() and fn.call() in JavaScript

var tempFn = function(someText){
console.log(someText);
}

tempFn('siva');
// where I simply call the function with text 'siva'

vs.

tempFn.call(this,'siva');
// where I call the function using call method

What is the difference between these approaches?

Upvotes: 2

Views: 1038

Answers (1)

Jonathan.Brink
Jonathan.Brink

Reputation: 25413

When you use the call form you are being explicit about the context that the function will be invoked with.

The context will determine what the value of this is when your function executes.

In your case, you are passing in this which would be the default anyway, so it's a no-op. Also, your tempFn function doesn't invoke the this keyword so it wouldn't matter anyway if you passed in a different scope.

Upvotes: 3

Related Questions