I don’t do JavaScript much yet have to write a WebExtension and soon after starting I found that OOP in JavaScript is very different form other language. So after few hours of search and study I came up with the following solution for achieving polymorphism in JavaScript.
var TClassAbs = function() { this.showId = function() { throw "Not Implemented"; }; } var TClass = function() { this.showId = function() { console.log(1); }; }; TClass.prototype = new TClassAbs(); TClass.prototype.constructor = TClass; var TClassEx = function() { var _this = this; var _parent = Object.getPrototypeOf(this); this.showId = function() { _parent.showId(); console.log("A"); }; }; TClassEx.prototype = new TClass(); TClassEx.prototype.constructor = TClassEx; var tObj = new TClassEx(); tObj.showId(); console.log(tObj);
Now, my question here:
- If the way I am inheriting is OK to use in production code?
- If the way I am accessing ‘parent object’ is OK to use in production code?
- If the way I am accessing ‘private member’ is bad?
And, I would also very much like to see possible improvement. Thanks.