0

我有一些奇怪的问题是箭头函数:

在原型方法(本例中为 es6 类方法)中,箭头函数应该具有 this 绑定的上下文,但在本例中,'this' 在第一个 lambda 中是 undef

 apply(bookings) {
    if (!bookings.length) {
      return
    }
    bookings.forEach(booking=> {

  //this is undef here

      let matchingTimeSlot = this.timeSlots.find(item=>item.bookingDate.isSame(booking.bookingDate))
    })

apply正在从另一个 es6 类调用:

this.days[i].apply(currentDaysBookings);

4

1 回答 1

2

这是 undef 这里

请注意,您的调试工具可能在骗您。this实际上会被转换为_this或类似的东西。您应该查看生成的 JavaScript。我可以向您保证,它将_this(或任何名称)指向正确的事物。

于 2016-03-22T05:17:55.630 回答