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;
};
}
本文介绍了一个用于解决Microsoft Internet Explorer 4和5中Function.apply方法缺失的问题的解决方案。通过提供一种替代实现,使得开发者可以在这些浏览器中正确地调用Function.apply方法。

4145

被折叠的 条评论
为什么被折叠?



