The this
keywords value depends on how the function is called. So if you assign that function to MODULE.sub
, and then invoke it as MODULE.sub(…)
, then this
will point to the MODULE
of course. If you had invoked it as myObject.method(…)
, then this
would point to that object.
So you either should expose the whole myObject
(like in @BingeBoys answer), or do not export that function and expect it to be a method of myObject
. Instead, you could use a bound one:
return myObject.method.bind(myObject);
or the explicit equivalent of that
return function() {
return myObject.method();
};
or you would not put the function as method
on that object at all:
…
var myObject = {
key: value,
};
return function() {
myObject.key // no `this` here
…
};
…