Extending Built-in Types

The Function.apply( ) method is missing in Microsoft Internet Explorer 4 and 5. This is a pretty
important function, and you may see code like this to replace it:

 

// IE 4 & 5 don't implement Function.apply( ).
// This workaround is based on code by Aaron Boodman.
if (!Function.prototype.apply) {
    // Invoke this function as a method of the specified object,
    // passing the specified parameters. We have to use eval( ) to do this
    Function.prototype.apply = function(object, parameters) {
        var f = this;                // The function to invoke
        var o = object || window;    // The object to invoke it on
        var args = parameters || []; // The arguments to pass
        // Temporarily make the function into a method of o
        // To do this we use a property name that is unlikely to exist
        o._$_apply_$_ = f;
     // We will use eval( ) to invoke the method. To do this we've got
     // to write the invocation as a string. First build the argument list.
     var stringArgs = [];
     for(var i = 0; i < args.length; i++)
         stringArgs[i] = "args[" + i + "]";
     // Concatenate the argument strings into a comma-separated list.
     var arglist = stringArgs.join(",");
     // Now build the entire method call string
     var methodcall = "o._$_apply_$_(" + arglist + ");";
     // Use the eval( ) function to make the methodcall
     var result = eval(methodcall);
     // Unbind the function from the object
     delete o._$_apply_$_;
     // And return the result
     return result;
  };
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值