views:

13

answers:

1
   var CoreGroups = new function(){
      this.name = function(){return 'name test'};

      var functionName = function(){
         // here I want to call the name() function
         a = this.name(); // doesnt work: this.name is not a function
         b = name(); // doesn't work too: name is not defined

      }
   }

Any idea on how to call the name() function from the functionName() function? Thanks! ;)

+1  A: 

Like this:

var CoreGroups = new function(){
    var self = this;

    this.name = function(){return 'name test'};

    var functionName = function() {
        var a = self.name();
    };
}
SLaks
That's not good. Image I have a Class named Thumb, and I have 100 thumbs, how I will set the self then? it's not the case of the example in my question.
Totty
Each instance of the class will have a separate `self` variable. (This code goes inside the function, as in my edit)
SLaks
It's not working like this :s
Totty
Why not? Please show us more code.
SLaks
Now it works! thanks ;)
Totty