julian
julian

Reputation: 63

Uncaught ReferenceError: "method" is not defined

I've created a javascript object

var Article = function(data) {
    this.foo = data,
    this.get_more_data = function() {
        // do something, get a response
        show_data(response);
    },
    this.show_data = function(bar) {
        //do something with bar;
    }
};

which works fine when the method show_data is written without this. but then it isn't accessible outside of the object. With this. I get a "Uncaught ReferenceError" from the Chrome console.

Why is this?

Thanks.

Upvotes: 2

Views: 10860

Answers (1)

Rob M.
Rob M.

Reputation: 36511

You should be calling show_data as a method of this, not as a function scoped to the current context:

var Article = function(data) {
    this.foo = data,
    this.get_more_data = function() {
        // do something, get a response
        this.show_data(this.foo);
    },
    this.show_data = function(bar) {
        console.log(bar);
    }
};

Upvotes: 1

Related Questions