1

I have a class along with its helper class defined:

function ClassA(){
    this.results_array = [];
    this.counter = 0;

    this.requestCB = function(err, response, body){
        if(err){
            console.log(err);
        }
        else{
            this.counter++;
            var helper = new ClassAHelper(body);
            this.results_array.concat(helper.parse());
        }
    };
};

function ClassAHelper(body){
    this._body = body;
    this.result_partial_array = [];
    this.parse = function(){
        var temp = this.parseInfo();
        this.result_partial_array.push(temp);
        return this.result_partial_array;
    };
    this.parseInfo = function(){
        var info;
        //Get some info from this._body 

        return info
    };
};

NodeJS gives me the following error:

TypeError: Object #<Object> has no method 'parseInfo'

I cannot figure out why I can't call this.parseInfo() from inside ClassAHelper's parse method.

If anyone can explain a possible solution. Or at least, what is the problem? I tried reordering the function declarations, and some other ideas, but to no avail.

P.S. I tried simplifying the code for stackoverflow. Hepefully it still makes sense :)

P.P.S This is my first stackoverflow question. Hopefully I did everything right. :)

nassan
  • 716
  • 7
  • 18
  • 1
    I am not familiar with node js, so i'm going to leave this here. My guess is you're using an anonymous function, so this isn't referring to the classAHelper this anymore, but the anonymous function. If you passed this in your function call as an argument, and then used it. this.parse = function(this){} – phpmeh Apr 09 '15 at 21:20
  • ^ this is correct, but I prefer using something like `var self = this` to keep track of `this` instead of passing it into the method – moarboilerplate Apr 09 '15 at 21:21
  • Thanks moar. Nassan, let us know if this works for you. – phpmeh Apr 09 '15 at 22:01
  • it's cool. I gave @moarboilerplate some rep love. – phpmeh Apr 12 '15 at 01:35

2 Answers2

1

Here's a simplified example which works:

function A() {
    this.x = function (){
        return this.y();
    };
    this.y = function (){
       return "Returned from y()";
    };
}

var a = new A();

a.x();

Notice the use of new and calling the method with a.x().

How are you creating an instance of your functions and calling parse in ClassAHelper?

Is it anything like these:

var a = A();
a.x();
// Or
A.x()
Adrian Lynch
  • 8,237
  • 2
  • 32
  • 40
0

this is scoped to the function it is inside. So, when you do this.parse=function(){, there is a new this. To keep ClassAHelper's this, you have to pass it in or reference it inside the anonymous function you made. The following example assigns this to a variable outside of the function and references it inside the function:

function ClassAHelper(body){
    this._body = body;
    this.result_partial_array = [];
    var self = this;
    this.parse = function(){
        var temp = self.parseInfo();
        self.result_partial_array.push(temp);
        return self.result_partial_array;
    };
    this.parseInfo = function(){
        var info;

        //Get some info from this._body 

        return info;
    };
};

Further reading and other ways of doing it: Why do you need to invoke an anonymous function on the same line?

Community
  • 1
  • 1
phpmeh
  • 1,752
  • 2
  • 22
  • 41
  • Not strictly true. It depends on what's done with ClassAHelper before hand. As a constructor function it will work fine. I think the instantiation code needs to be seen. – Adrian Lynch Apr 09 '15 at 21:32
  • @AdrianLynch It is used as a constructor function so it works fine. Thanks! – nassan Apr 12 '15 at 01:19
  • @nassan you can still use the checkmark to mark it as the answer – moarboilerplate Apr 13 '15 at 16:25
  • @phpmeh since `self` is defined outside the scope of the function `function(self)` is actually counterproductive and can lead to errors since it redefines self. Best to keep the function parameterless and completely dependent on the self variable defined outside the function closure. – moarboilerplate Apr 13 '15 at 16:28