箭头函数里 this 指向 window,而 apply 和 call 也无法对 this 进行绑定
function Cat(name) {
this.name = name;
}
Cat.prototype.say = function() {
return `Hello, ${this.name}!`;
// Cat.prototype.say = () => `Hello, ${this.name}!`; // this 指向 window
箭头函数没有this,如果有的话,这里的this也是来自于它的父级;
并且箭头函数没有function本身的arguments
Sign in to make a reply
_时空秋千
箭头函数里 this 指向 window,而 apply 和 call 也无法对 this 进行绑定
function Cat(name) {
this.name = name;
}
Cat.prototype.say = function() {
return `Hello, ${this.name}!`;
}
// Cat.prototype.say = () => `Hello, ${this.name}!`; // this 指向 window