6

Essentially I'd like to do some setup work if my library classes are called. For example:

class Child extends Parent { //methods } 

I'd like to assign a function to be called when the Parent class is extended. I'd like to be notified somehow. Before it's about to happen (with the methods about to be attached as a parameter), or after with the Child class as a parameter.

I have an ES5 library I built that uses a factory function to create new classes, and in that function I do lots of setup work. I'd like to do all that same stuff but with the simplicity of the ES6 class syntax so developers using my library don't need to think anything special is going on and can think in terms of more straightforward classes.

Any help would be much appreciated.

4
  • Raise an event in the Parent constructor or have a method that child classes are forced to implement, such as onConstruct(); where you can trigger a custom event. However, since you are hiding complexity behind magic, I don't think this is the way you want to go. If you have a factory that does certain setup, don't you think the people using your library should be aware of it? I would implement a method that every child executes after constructing - that will hint something goes on and people will be aware it's not just a "regular" class they're dealing with. Commented Oct 8, 2015 at 12:30
  • but I actually need the "work" code to run just when the Parent is extended, not on instantiation of objects. Commented Oct 8, 2015 at 12:45
  • The parent isn't "extended" until a new object is created using the child class. You can create as many classes that extend something - that doesn't mean they are in use. They are in use when you do something with them, and you perform that action when you create a new object. You can't be notified that a blueprint took parts of another blueprint, it makes no sense. You can visually confirm that building B contains traits of building A because they stem from the same main blueprint. Commented Oct 8, 2015 at 12:57
  • 1
    I think you will have to provide more information about your use case. It doesn't make a lot of sense to me in its current form. Commented Oct 8, 2015 at 13:22

2 Answers 2

0

I think that what @Mjh is trying to say is that you can implement in the parent class a hook. Classes extending the Parent class will be able to override the method. If they dont want to do anything with it... it's also fine.

https://jsbin.com/navijusuzo/1/edit?js,console

class Parent { constructor(){ this.setup(); this.afterConstruct(); } setup(){ // Do stuff, validate, init object } // HOOK to be overriden by classes that extend Parent afterConstruct(){ } } class Child extends Parent { constructor(){ super(); } // Override: Implements Hook afterConstruct(){ console.log('Child hooking') } } new Parent(); new Child(); 
Sign up to request clarification or add additional context in comments.

Comments

-1

You can define callback in your ES6 constructor function as:

class Point { constructor(x, y, cb) { this.x = x; this.y = y; if (typeof cb === 'function') return cb(); //If Callback function is available, execute it } } class ColorPoint extends Point { constructor(x, y, color, cb) { super(x, y, cb); //Pass callback to parent constructor this.color = color; } } let cp = new ColorPoint(25, 8, 'green', function () { alert('callback called') }); //Callback function passed 

Fiddle Here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.