4

Is there a way for a javascript method to reference the environment in which it was defined as opposed to how it was called? In this particular case - the method is defined in an object literal.

ie:

var obj = {
            init: function(){
                  jQuery("form").on('keyup','input',{self:this},this.checkDetails);
            }
            checkDetails: function(event){

                  console.log(this); 
                  // 'this' references the input object which invoked the method

                 console.log(event.data.self); 
                  //provides a correct reference to the object 'obj', but I had to pass it
            }

     }

this should reference the generic object in which it was defined.

andy
  • 41
  • 3
  • Replace `console.log(event.data.self);` with `console.log(obj)`? Or are you looking for a more generic solution? – merlin2011 Jul 08 '14 at 08:53
  • @merlin2011 This makes it necessary to let the value of the variable `obj` unchanged – Denys Séguret Jul 08 '14 at 08:55
  • I am looking for more generic solution – andy Jul 08 '14 at 08:57
  • @dystroy - the method is defined in an object literal, which is not covered by the answer you indicated. – andy Jul 08 '14 at 09:18
  • *" javascript method to reference the environment in which it was defined"*. An object literal isn't an environment. See also [this other related question](http://stackoverflow.com/questions/4858931/reference-variable-in-object-literal). – Denys Séguret Jul 08 '14 at 09:20

0 Answers0