Trying to get to grips with OO prototypal vanilla javascript inheritance coming from c# background.
I have a base "class" that does a HTTP GET. I want to inherit from this so that sub classes can encapsulate variations of this HTTP GET.
In the following code I get a httpGet is undefined error in the getUser function. My end goal is to get a call to a nice simple method such as the userComms.getUser()
// declare base class
function HttpComms() { }
HttpComms.prototype.httpGet = function (url, callback) {
// Code to do http get request
};
// declare sub class
function UserComms() {
this.getUser = function () {
// error thrown here:
httpGet('user/get', function (data) {
console.log(data);
});
}
}
// hookup prototypal relationship between base and sub
UserComms.prototype = Object.create(HttpComms.prototype);
// create and call sub class.
var userComms = new UserComms();
userComms.getUser();
Why do I get the error and what would be the most flexible way to get this working?