I am not sure if JS modules are supposed to work like this, but it doesn't make sense. I am using React and I would like to export an object from one module and then call methods on that object. This work well so far, however, the issue is that inside the invoked method, this
doesn't point to the object on which it was invoked, and I can't access other member methods. Here is an example:
export let localService = {
getData: () => {
console.log(this);
this.testMethod();
},
testMethod: () => {
}
};
In the other file I import the object and invoke the method:
import {localService} from '../../localService.js';
localService.getData();
Now things get strange, inside the getData
I can't access this.testMethod()
directly because this
is sort of different, here's the output of console.log(this)
and of course invoking this.testMethod()
throws an error saying that method doesn't exist. I am able to access testMethod()
like this: this.a.testMethod()
, but where does this a
come from? So is this normal behavior, or maybe it's React's fault ?