How do I re-create "this" inside an event handler?

I've got an event handler that was pre-bound to a specific variable (via $.proxy). As a result, when the handler is triggered, this isn't the normal value, it's my pre-bound value.

I'd like to recover this using the handler's event argument, but this doesn't seem to map directly to event.currentTarget, event.target, or any other event property.

So, I've tried digging through the jQuery source, but the event callback stuff is very convoluted, and I can't figure out exactly what this is getting set to. Does anyone know how simulate a jQuery event handler this using only the event argument?

* * Edit * *

Just to clarify, here's an example:

var boundThis = {foo: 'bar'}
var handler = $.proxy(function(event) {

    // Because of the $.proxy, this === boundThis
    // (NOT the normal "this" that jQuery would set)
    // In theory event has everything I need to re-create this,
    // but I'm having trouble figuring out exactly how

    // Here's a naive/non-functional example of what I'm trying to do
    jQueryThis = event.target; // If only this worked ...

}, boundThis);
$(someElement).click(handler);

Answers


event.currentTarget is typically the value of this with jQuery events. As described in the docs:

Description: The current DOM element within the event bubbling phase.

Demo: http://jsfiddle.net/rhgEB/.

While event.target remains as #baz, event.currentTarget references the current element being handled; same as this does without a proxy.

* * Edit by machineghost * *

Just to save future readers some time, the "magic formula" for generating a this equivalent based on an event object ("e") is:

var fakeThis = e.delegateTarget === e.currentTarget ? e.currentTarget : e.target;

Need Your Help

Finding user location before map loads

iphone cocoa-touch mapkit

I'm trying to find the user's location using:

Efficient copy of entire directory

c linux unix

I want to copy one directory and the two files under it to another shared location of shared storage. Is it possible to combine the three(one directory and two files) as a continuous file writing and