Do you simply want to correct the "this" keyword?

var doSomething = function() {
    return function(e) {
        e = e || window.event;
        this.style.background = '#000';
    };
}.call(div);

div.onclick = doSomething;

On Dec 15, 1:03 am, Yu-Hsuan Lai <[email protected]> wrote:
> When I globally invoke a function, the this inside it will refer to window.
> I have read "Good Part" so I know to workaround it by another variable
> "that". But see below example:
>
> I want to append some code to an event handler:
>
> aDiv.onclick = function () {
>     var tmpFunc = aDiv.onclick;
>     return function () {
>         tmpFunc();
>         someAnotherFunc();
>    }
>
> }();
>
> It almost works well. But if origin onclick contain a this, it will refer to
> window because tmpFunc() is a global invocation.
> I found a solution, but I think it's not clever:
> (If possible I wouldn't like to change origin onclick because it's in
> another file)
>
> aDiv.onclick = function () {
>     var tmpFunc = aDiv.onclick;
>     var tmpObject = aDiv;
>     return function () {
>         tmpFunc.apply(tmpObject);
>         someAnotherFunc();
>    }}();
>
> --
> Lai, Yu-Hsuan

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to