How do I create an abstract base class in JavaScript?
问题 Is it possible to simulate abstract base class in JavaScript? What is the most elegant way to do it? Say, I want to do something like: - var cat = new Animal('cat'); var dog = new Animal('dog'); cat.say(); dog.say(); It should output: 'bark', 'meow' 回答1: One simple way to create an abstract class is this: /** @constructor @abstract */ var Animal = function() { if (this.constructor === Animal) { throw new Error("Can't instantiate abstract class!"); } // Animal initialization... }; /**