Is there a difference between `this.function()` and `function.call(this)`?

const person = {
  name: "Bob",
  greet: function() { console.log("Hello " + this.name) }
};

const thank = function() {
  console.log("Thanks " + this.name);
}

person.greet() is the same as person.greet.call(person), but the first is more succinct, so this is why this variant exists.

call function is useful when function is not member of the object. You can't call person.thank(), you must call thank.call(person).

Tags:

Javascript