DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Implement Function.apply( ) In IE 4 & 5

11.27.2009
| 2966 views |
  • submit to reddit
        
    IE 4 & 5 don't implement Function.apply( ).
   This workaround is based on code by Aaron Boodman.
  Quoted from JavaScript,the Definitive Guide,5th edition
   


    if (!Function.prototype.apply) {
    Function.prototype.apply = function(object, parameters) {
    var f = this; 
    var o = object || window;
    var args = parameters || []; 


    o._$_apply_$_ = f;


    var stringArgs = [];
    for(var i = 0; i < args.length; i++)
    stringArgs[i] = "args[" + i + "]";


    var arglist = stringArgs.join(",");


    var methodcall = "o._$_apply_$_(" + arglist + ");";


    var result = eval(methodcall);


    delete o._$_apply_$_;

    return result;
    };
    }