0

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?

1 Answer 1

3

Note this. in front of httpGet:

function UserComms() {
    this.getUser = function () {
        // error thrown here:
        this.httpGet('user/get', function (data) {
            console.log(data);
        });
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.