IE4 & IE5 添加apply()方法,IE4 & IE5 没有Function.apply()方法

// IE 4 & IE 5 不包含 Function.apply()
// IE 4 & IE 5 don't implement Function.apply();

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
    // 为指定的对象调用这个方法,需要三个参数。我们实现这个方法要使用eval()

    Function.prototype.apply = function (object, parameters)
    {
        var f = this;                           // The function to invoke
        var o = object || window;               // The function 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 对象的一个临时属性创建 o 对象的一个临时方法

        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.
        // 我们必须写一个字符串用eval()方法运行.首先创建参数列表

        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
            // 用eval()运行该方法

            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、付费专栏及课程。

余额充值