我知道使用.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
?