javascript - Avoiding var _this = this; when writing jQuery event handlers -
not important question, here go..
how avoid using var _this = in jquery event handlers? i.e. don't doing:
var _this = this; $(el).click(function (event) { //use _this access object , $(this) access dom element });
the following 2 ways not ideal
$(el).click($.proxy(function (event) { //lost access correct dom element, i.e. event.target not enough (see http://jsfiddle.net/ne3n3/1/) }, this)); $(el).click({_this: this}, function (event) { //have access $(this) , event.data._this, seems verbose })
ideally like
$(el).click(function (event) { this.method(); // accessing object event.realtarget; // accessing dom element }, this); // <= notice
http://api.jquery.com/event.currenttarget/ says:
"this property typically equal of function."
Comments
Post a Comment