1

I know about using .call() and .apply() to set the this for a function call, but can I use it for a lambda?

Consider this example from the MDN webdoc for Function.prototype.call:

function greet1() {
    var reply = [this.person, 'Is An Awesome', this.role].join(' ');
      console.log(reply);
}

let greet2 = () => {
    var reply = [this.person, 'Is An Awesome', this.role].join(' ');
      console.log(reply);
}

var i = {
    person: 'Douglas Crockford', role: 'Javascript Developer'
};

greet1.call(i);  // Douglas Crockford Is An Awesome Javascript Developer
greet2.call(i);  // Is An Awesome Javascript Developer

Why doesn't the greet2 call work the same was as greet1 and is there a way to set this in the case of greet2?

ErikR
  • 51,541
  • 9
  • 73
  • 124
  • 4
    ["Since arrow functions do not have their own this, the methods call() or apply() can only pass in parameters. thisArg is ignored."](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) – Andy Nov 18 '17 at 18:36
  • @Andy — _Perfect!!!_ – Rayon Nov 18 '17 at 18:37
  • Really boils down to they are a convenience shortcut but can't be used in all situations – charlietfl Nov 18 '17 at 18:39

0 Answers0