I'm trying to wrap my head around objects and object literals. I can't
seem to figure out how to access the object within one of its methods,
when the method is triggered by an event. In other words the context
for 'this' shifts when the method is invoked by a handler. See simple
example below:


$(document).ready(function () {

var myObject = {
        o : 'hello',
        init : function() {
                alert(this.o); //'this' changes
        }
}

myObject.init(); // 'hello'
$(document).click(myObject.init); //'undefined'

});

When the click event calls the init() method, this in alert(this.o)
changes to the document. Is there a way to make 'this.o' always refer
to the object?

thanks for any help

--dave

Reply via email to