Last active
February 1, 2023 05:54
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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