1

I would like to create an abstract class in javascript, which implements certain methods, calls abstract ones and let the 'inherited' classes to implement these abstract ones. I've read a lot about js prototyping. Every single suggested implementation of abstract classes and methods in javascript seems to be a simple inheritance, but not real abstraction. Here is a really simple example to show what i want to achieve:

    var abstractClass = {
      abstractMethod: function() { /* i don't know yet what i'm going to do */ },
      concreteMethod: function() {
        abstractClass.abstractMethod();
      }
    }


    specializedClass = Object.create(abstractClass);
    specializedClass.abstractMethod = function() {
      alert('Now i know what to do');
    }

    specializedClass.concreteMethod();

My question: is there a non-hacky, non-workaround way to make abstract classes in javascript?

4
  • Those are instances, not classes. Commented Jan 29, 2016 at 20:50
  • I would avoid trying to mould JavaScript into a classical language. Really there is no such thing as a class in js, even though we have the class keyword in ES6/2015 it's all just syntactic sugar. Check this out: medium.com/javascript-scene/… Commented Jan 29, 2016 at 20:51
  • I don't really want to build class inheritance, I just want to make some kind of timed service, which handles the interval start/stop and the user classes just have to implement the action on the ticks Commented Jan 29, 2016 at 20:55
  • @kexx - in that case a better pattern would be to inject the callback (action...) to the timer constructor and not rely on implementation of a specifically named function. Commented Jan 29, 2016 at 20:58

1 Answer 1

1

inside the methods, use this instead of the named class/variable, which is duplicated/broken by your Object.create() call.

var abstractClass = {
  abstractMethod: function() { /* i don't know yet what i'm going to do */ },
  concreteMethod: function() {
    this.abstractMethod(); // < made generic with this 
  }
}


specializedClass = Object.create(abstractClass);
specializedClass.abstractMethod = function() {
  alert('Now i know what to do');
}

specializedClass.concreteMethod();
Sign up to request clarification or add additional context in comments.

4 Comments

The point is the same when you use abstract methods in a real oop language: you may want to call it in the base class. Maybe in case of js it's not required, but i put it there for the sake of the example
@kexx: look into super, and the new explicit construction of ES6 classes for more ergonomic OOP coding. the problem with hard-coding is that Object.create() causes the new method to be defined on a new object, specializedClass, instead of on specializedClass.prototype, where your late modification would make it work. as is, the concreteMethod on specializedClass.prototype doesn't inherit from the own property method abstractMethod in the instance.
@kexx - wrong. An abstract function doesn't have implementation in "real OOP", it's just declared with the signature.
@Amit: i didn't say the abstract function has to be implemented, but an abstract class could contain concrete function implementations which call the abstract function

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.