Skip navigation.

My comments on ...

... everything bothering me

Wrapping DOM functions — revisited

, ,

A while back I "discovered" a way to wrap DOM functions in IE to make it more alike normal Ecmascript functions. With valuable input from liorean I found this to be the only workable solution (sadly)...

edit: Just to make this clear: You can only use this method to call functions on objects that already possess a method of that name.

This one does use a closure and I bet it does leak memory in IE... I just don't care P:

function wrapDOMf (scope,func) {
    /* ensure proper usage (no type checking though) */
    if(!(scope && typeof func == "string" && scope[func]))
       return;

    scope[func] = function(){
        return Function.prototype.apply.call(this[func],this,arguments);
    };
}


The new versions of wrapFunc and wrapFuncBefore were changed in a similar way:

function wrapFunc (scope,func,hijack) {
    if(!(scope && typeof func == "string" && scope[func]))
       return; //ensure proper usage

    if(!(hijack && hijack instanceof Function))
       hijack = function(x){return x}; //if no hijack exists, f(x)=x

    scope[func] = function() {
        var obj = Function.prototype.apply.call(this[func],this,arguments)
        /* 'hijack' is passed the output object and all the arguments */
        return hijack.call(this,obj,arguments);
    };
}


function wrapFuncBefore (scope,func,hijack) {
    if(!(scope && typeof func == "string" && scope[func]))
       return; //ensure proper usage

    if(!(hijack && hijack instanceof Function))
       hijack = function(){return [this,arguments]};

    scope[func] = function() {
        var temp = hijack.apply(this,arguments);
        var that = temp[0];
        var args = temp[1];
        return Function.prototype.apply.call(that[func],that,args);
    };
}


Feel free to correct where I'm wrong.

The "classic example" — revisitedDrop in average intelligence on MyOpera

Write a comment

You must be logged in to write a comment. If you're not a registered member, please sign up.

December 2009
M T W T F S S
November 2009January 2010
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31