Skip to content

Instantly share code, notes, and snippets.

@bheesham
Last active February 1, 2023 05:54
Show Gist options
  • Save bheesham/5138050 to your computer and use it in GitHub Desktop.
Save bheesham/5138050 to your computer and use it in GitHub Desktop.
A somewhat simple way to have abstract classes in JavaScript. There's probably a better way to do this.
// Define an abstract class
var Abstract = function() {};
Abstract.prototype.f1 = function() {
throw "Not implemented";
}
Abstract.prototype.f2 = Abstract.prototype.f3 = Abstract.prototype.f1;
// Implement our abstract class
var Class = Abstract;
Class.prototype.f1 = function() {
return 1;
}
Class.prototype.f2 = function() {
return 2;
}
// Use it
var test = new Class();
// Debug messages and stuff
console.log(test instanceof Class);
console.log("Result of f1(): " + test.f1());
console.log("Result of f2(): " + test.f2());
test.f3();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment