0

In my main.js, I have:

import listeners from "./listeners"
listeners.call(this)

and my listeners.js is:

import _ from "lodash"

const listeners = () => {
  console.log("this", this)
}

export default listeners

When it runs, it doesn't have the right this value in the listeners function. What am I doing wrong?

Shamoon
  • 41,293
  • 91
  • 306
  • 570
  • 3
    change the arrow function to a traditional `function() {...}` - see also https://stackoverflow.com/questions/33308121/can-you-bind-arrow-functions – le_m May 26 '17 at 01:52
  • in general, you can use Function's `bind` method (which doesn't technically change the `this` in the function, it creates a new function with `this` set as you require) – Jaromanda X May 26 '17 at 02:04

1 Answers1

1

It doesn't have the right this since an ArrowFunction does not define local bindings for this.

You could find some information here and the spec here.

PoppinL
  • 446
  • 3
  • 7