0

I have an node library and want to override a method in it but i don't understand how this particular object structure works.

function MyObject(){
    this.init();
}
MyObject.prototype.init = function(){
    // tons of other stuff
    function myMethod(){
         // stuff I want to override
    }
}

Overriding the init function would be pointless because there is too much stuff in there.
I could just edit the lib but that's dirty and I want to prevent that if possible.
I tired all sorts of stuff but it didn't seem like i got it right. Is it even possible?

2
  • I would inject your dependencies in your constructor then call them from your init. Commented Mar 18, 2015 at 8:23
  • I have to admit that i can't follow you. would that be a possible solution or just a way to work around that which works sometimes? I'm not using that object directly, it has a complete functionality in itself. Commented Mar 18, 2015 at 11:04

1 Answer 1

1

myMethod is a "private" method of the init function, you can't get a reference to it from outside. So if you don't have control over the code defining this (you say you don't want to edit the lib), you can't override the method.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! seems like i have to stick to external library editing. too bad.
If you do end up modifying the library, it should be easy enough to make your changes backwards compatible: init can accept an optional function param, which, if present, will override myMethod.
thats not the point, i would add only a single line at the end of the method. but i cant publish something like that and simply tell "you need this dep, everything else will be handled by the script"
To the contrary, many JavaScript modules will take a config object which specifies dependencies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.