Let's say we have an object that looks like this:
var foo = {
bar: function(s){
return s + ' world!';
}
}
What is the difference between calling the foo.bar()
method like:
var greet = foo.bar.call(foo, "Hello");
vs
var greet = foo.bar("Hello");
What I understand from reading this doc is that the first parameter of the the call method basically sets the this
context of the method being called. But is there a benefit to using call
to execute foo.bar()
method in this case? In what cases should I consider using call
to execute a function?
Also, when is it necessary to pass undefined
as the first parameter to the call
method? For example:
echo.call(undefined, "print me");