views:

125

answers:

6

I've created an Object, and have a method setup() in the object.

this.debug = function (){...}

this.setup = function(){    

  var fieldsets = form.children("fieldset");

  fieldsets.each(function(){        
    this.debug($(this).attr("class")));
  });


}

I'm trying to call this.debug which is in the scope of the Object but not in the scope of each, since THIS is a different this...

How do I access this.debug?

+3  A: 

Say var that = this after this.debug, then do that.debug.

Skilldrick
that didnt work =] lol
codeninja
Sorry if I'm missing the joke, but did it actually not work?
Skilldrick
this and that... that.funny = 101
neatlysliced
this should work. Put that = this at the beginning of the setup() method. (By work, I mean invoke the debug() method correctly, it could still throw an error.)
olooney
you really want do `var that = this`. Leaving out the var is a bad idea in general.
Kyle Butt
Thanks @Kyle - it's been a while since I last wrote any JavaScript :P You forget these little details...
Skilldrick
A: 

In jQuery 1.4 you can do:

this.debug = function (){...}

this.setup = function(){    

  var fieldsets = form.children("fieldset");

  fieldsets.each(jQuery.proxy(function(){
    this.debug($(this).attr("class")));
  },this);
}

The jQuery.proxy(function, object) function will take 2 arguments:

  • The function will be the function used in the loop.
  • The object argument will be the this object inside the function.

In this way, you can transfer the this from the outer scope inside the each function.

Scharrels
Then both this's will be the outer this...
Skilldrick
+2  A: 

This is basically Skilldrik's answer, but showing you where it works best

this.setup = function(){    
  // save it in a scoped var...  some people use "self" for this purpose, i prefer
  // naming it whatever the outer object actually is...
  var containingObject = this;

  var fieldsets = form.children("fieldset");

  fieldsets.each(function(){        
    // use that scoped var later!
    containingObject.debug($(this).attr("class")));
  });      
}
gnarf
+1. Also, if your debug method doesn't use instance data, you can call it in a static context: ContainingObject.debug($(this).attr("class")));.
markalex
A: 

Hey,

I tried this in my Greasemonkey-Console:

this.debug = function() {
    console.log("foo");
};

this.setup = function() {

    var fieldsets = form.children("fieldset");

    fieldsets.each(function(){
        debug($(this).attr("class"));
    });
};

Which will search the scope for any debug .. which that is hopefully the function above. This will fail, if you assign a variable with the very same name :)

mana
`this` in the `foreach` loop will contain the fieldset object of the current iteration.
Scharrels
A: 

I normally do it this way: (note:example below is from memory but looks sound):

function CustomObject()
{
  var _instance = this;

  this.debug = function(){...};
  this.setup =
    function()
    {    
      var fieldsets = form.children("fieldset");
      fieldsets.each(
        function()
        {        
          _instance.debug($(this).attr("class"));
        });
    };
}
theahuramazda
A: 
this.debug = function (){...}

this.setup = function(){    
  var that = this;
  var fieldsets = form.children("fieldset");

  fieldsets.each(function(){        
    that.debug($(this).attr("class")));
  });


}
svinto