0

I am currently trying to figure out a way to assign methods to the super object in a class, in order to extend a functionality inside a class.

I want to create a "Component" class and have each class that extends the "Component" class have different methods depending on the component needs. I want to use the term "describe" for the method that would extend the super object. Therefore, I am describing the component.

Here is an example:

class Component {
    constructor (args) {
        this.template = args.template;
    }

    getTemplate () {
        return this.template;
    }

    describe () {
    //The magic should happen here
    }
}

Class Controller describer

class Controller {
    constructor () {

    }

    getEvents () {
        return {};
    }
}

Extending the Component class. Then, using the "describe" method to inject other methods from other classes.

class example extends Component {
    constructor () {
        super({template: '<div></div>'});
        super.describe({
            Controller
        });

        super.getTemplate();
        super.getEvents();
    }
}

var app = new App({
    example
});

Is it possible? Thank you :)

Gil Nimer
  • 1
  • 2
  • Do you want to extend `Component` or `example`? `super` refers to `Component` so extending `super` will extend all future classes that extends `Component`. It is possible but I'm not sure if that is what you want – slebetman Apr 01 '17 at 00:56
  • You don't extend/subclass by adding methods to `super`. You extend by adding methods to either a prototype that inherits from the super object or by adding methods directory to a newly created object in the constructor. – jfriend00 Apr 01 '17 at 01:04
  • I understand, but I wanted to be able to add methods dynamicaly without affecting other classes which extends the "Component" class. So I assume the only way I can do it is to assign the methods to "this" right? Something along the line: https://jsfiddle.net/wea4bkxf/ – Gil Nimer Apr 01 '17 at 03:20
  • Yes, assign methods to `this` in the constructor. Then those methods belong only to that particular object (or any other objects that run that same code). `this.myMethod = function() {...}`. – jfriend00 Apr 01 '17 at 05:44
  • Thank you, I guess coming from other OOP backgrounds like php makea the "super" syntax looks somewhat weird at first, but well, we all like Javascript :) – Gil Nimer Apr 01 '17 at 07:56
  • Possible duplicate of [Is it possible to undo Object.assign?](https://stackoverflow.com/questions/29462802/is-it-possible-to-undo-object-assign) – Paul Sweatte Jul 29 '17 at 23:33

0 Answers0