2
var Foo = require('Foo');
Foo.prototye.componentDidMount = () => {};

I import Foo, but for the case that I use it, I want to override the componentDidMount function on it's prototype. I know this is a hack : (

If I do this, I worry that in other cases where Foo is used, it's prototype will also be changed.

Is there a way to essentially "scope" changing the prototype to within this file?

3
  • yes, though this is in the context of reactjs. updated question to be more clear Commented Jun 1, 2015 at 20:54
  • what do you want to do that? Commented Jun 1, 2015 at 21:14
  • because I'm essentially 'not allowed' to change the Foo component. But, I want everything about the Foo component, except for it's componentDidMount : ( Commented Jun 2, 2015 at 1:13

1 Answer 1

1

No. The constructor exists globally. You can, however, make a subclass of Foo if you have access to its constructor. In your example, it seems like a Foo instance is being imported, not a constructor, so I'm not sure yet that's an option. But here's what you'd do.

function Bar() {
  // ...
}
Bar.prototype = new FooConstructor(); // Must be a separate instance!
Bar.prototype.bar = () => {};
var MyFooExtension = new Bar();

EDIT: Forgot, most objects should have a constructor property. So you may be able to replace this line:

Bar.prototype = new Foo.constructor(); 
Sign up to request clarification or add additional context in comments.

Comments

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.