2
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?

beaker
16.9k3 gold badges36 silver badges53 bronze badges
asked Mar 16, 2016 at 18:38
1

1 Answer 1

3

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.

answered Mar 16, 2016 at 18:40
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.