Given the following, common scenario:
console.log(this); // window or any parent object
$('.selector').on('click', function(event) {
console.log(this); // clicked DOM element
});
var myFunc = function() {
console.log(this); // window or parent object
}
Since version 1.3 jQuery adds the event.currentTarget
when binding event handlers for which counts event.currentTarget === this
, so is there actually a good reason to manipulate this
and switch context? Doesn't this behaviour generally go against the unspoken rule of "don't change keyword values" (like undefined = 'not defined'
)?
This "feature" of jQuery makes a lot of OOP less efficient and awkward imho, when we need to either cache the original this
in a variable like self
or use helpers like jQuery.proxy
to reassign context to event handlers.
My question: is this just a relic of early jQuery implementations kept alive or is there an actual benefit which I cannot see (except maybe the slightly more convenient way than accessing event.currentTarget
to get the element...)?