Reputation: 1564
I have a package (node module) which has some methods, but unfortunately they are constructed in a strange way. All methods refer to a variable which only exists inside the package (local var).
So for example:
var theVar = {};
module.exports.foo = function() {
theVar.someMethod();
}
Well, I would like to override some of these methods in my new lib, but cannot do it because of theVar. Its value of course points to the old stuff, but I need to make it point to a new object.
Could someone tell me how to override such methods? Is this even possible in JS?
Thanks in advance
EDIT
So just to make sure I understand this correctly:
theVar is actually defined like this:
var theVar = module.exports.theVar = {....}
However, all the methods of the module use it like:
exports.func = function() {
theVar.method() // and not exports.theVar.method()
}
So this means, theVar cannot be changed, right? I could only change exports.theVar.
Upvotes: 1
Views: 2771
Reputation: 235992
If theVar
gets return
'ed somewhere as a whole (its reference, so to speak), then yes, you can just overwrite any method / property contained by it (if those properties were not made imuteable by Object.freeze()
or other methods).
If that object is not made public somewhere, by passing its reference around, then no, there is no way in ECMAscript to break into an unknown context. In other words, you can't access a closured variable from anywhere, but a child context.
Upvotes: 2