关于`.call()`的`function`和`()=>`之间的区别?

时间:2017-11-18 18:32:01

标签: javascript anonymous-function

我知道使用.call().apply()为函数调用设置this,但是我可以将它用作lambda吗?

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

为什么greet2调用与greet1的调用不相同?在this的情况下,有没有办法设置greet2

0 个答案:

没有答案