0

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");
Owen Lilly
  • 171
  • 3
  • 10
  • Refer [___`Function.prototype.call()`___](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call) – Rayon Aug 23 '16 at 05:46
  • `Let's say we have an object that looks like this:` - then you wouldn't have an object at all as that is not valid javascript – Jaromanda X Aug 23 '16 at 05:48

0 Answers0