Discuss / JavaScript / 用箭头函数怎么写?

用箭头函数怎么写?

Topic source

_时空秋千

#1 Created at ... [Delete] [Delete and Lock User]

箭头函数里 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

一库嘟

#2 Created at ... [Delete] [Delete and Lock User]

箭头函数没有this,如果有的话,这里的this也是来自于它的父级;

并且箭头函数没有function本身的arguments


  • 1

Reply