-1

I saw code:

Host.call(this, logger, config);

Host is a function name with no parent.
call seems not defined in the function's definition.

Is call a special function? As call is a highly used word, it seems not easy to search "JS call" for any useful explanation.

Wason
  • 1,341
  • 1
  • 13
  • 26
  • Well.. It calls function. (`Host` in your example) – Maxx Oct 10 '16 at 08:18
  • 2
    This should help: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call. In simple words, it will change the context of function and call it. Another reference link: http://stackoverflow.com/questions/15455009/javascript-call-apply-vs-bind – Rajesh Oct 10 '16 at 08:18
  • asked and answered countless times on SO – Jaromanda X Oct 10 '16 at 08:24

2 Answers2

1

All functions have two built-in methods that allow the programmer to supply arguments and this variable also: call and apply.

So in your question, the Host method is being called with thelogger and config arguments, but you're also passing in a reference to whatever is currently this.

Graham
  • 7,431
  • 18
  • 59
  • 84
WheretheresaWill
  • 5,882
  • 7
  • 30
  • 43
1

.call() sets this within Host function to this passed at first parameter where logger, config are additional parameters passed to Host function

function Host(a, b) {
  // `this` : `obj`
  console.log("in Host", this, a, b);
  this.a = a;       
  this.b = b;
}

var obj = {};

Host.call(obj, 1, 2);

console.log(obj.a, obj.b); // set at `obj` : `this` at call to `Host`
guest271314
  • 1
  • 15
  • 104
  • 177