Javascript: How To Access Object Member From Event Callback Function
I have some problems trying to figure out what is wrong with my object design. var comment = function(){ var textarea = null; $(document).ready( init ); function init() { $('.
Solution 1:
Since inside those handlers the context will change, it's easiest to keep a reference to the context you want, I personally prefer self
. Here's one alternative format:
var comment = function(){
this.textarea = null;
var self = this;
$(document).ready( init );
functioninit()
{
$('.reply').click( reply_to );
self.textarea = $('.commentbox textarea');
}
functionset_text( the_text )
{
self.textarea.val( the_text );
}
functionreply_to() {
set_text( 'a test text' );
}
}();
You can test it here. Admittedly though I'm not really sure what you're trying to accomplish though. You are trying to return the reply_to
function, but bind it yourself inside the init()
ready handler...so you can either bind it immediately (like above), or change it up and return what you want to bind elsewhere.
Post a Comment for "Javascript: How To Access Object Member From Event Callback Function"