Apply and call of Function.prototype were only added in ECMA262 Edition3 released in 1999 (ECMA262 Edition2 was released in 1998). In previous browsers such as IE5.01 (JScript 5.0), there is no apply and call. This will cause some compatibility issues. Here are the fixes:
Copy the code code as follows:
if(!Function.prototype.apply){
Function.prototype.apply = function(obj, args){
obj = obj == undefined ? window : Object(obj);//obj can be a js basic type
var i = 0, ary = [], str;
if(args){
for( len=args.length; i<len; i++ ){
ary[i] = "args[" + i + "]";
}
}
obj._apply = this;
str = 'obj._apply(' + ary.join(',') + ')';
try{
return eval(str);
}catch(e){
}finally{
delete obj._apply;
}
};
}
if(!Function.prototype.call){
Function.prototype.call = function(obj){
var i = 1, args = [];
for( len=arguments.length; i<len; i++ ){
args[i-1] = arguments[i];
}
return this.apply(obj, args);
};
}