完美解決IE低版本不支持call與apply的問題
更新時間:2013年12月05日 09:53:25 作者:
這篇文章主要是對解決IE低版本不支持call與apply的問題進行了詳細的介紹。需要的朋友可以過來參考下,希望對大家有所幫助
Function.prototype的apply和call是在1999年發(fā)布的ECMA262 Edition3中才加入的(1998年發(fā)布ECMA262 Edition2)。在此前的的瀏覽器如IE5.01(JScript 5.0)中是沒有apply和call的。因此會帶來一些兼容性問題,以下是修復(fù)方式:
if(!Function.prototype.apply){
Function.prototype.apply = function(obj, args){
obj = obj == undefined ? window : Object(obj);//obj可以是js基本類型
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);
};
}
復(fù)制代碼 代碼如下:
if(!Function.prototype.apply){
Function.prototype.apply = function(obj, args){
obj = obj == undefined ? window : Object(obj);//obj可以是js基本類型
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);
};
}
相關(guān)文章
javascript中常見的3種信息提示框(alert,prompt,confirm)
2012-09-09javascript轉(zhuǎn)換靜態(tài)圖片,增加粒子動畫效果
這篇文章主要介紹了javascript轉(zhuǎn)換靜態(tài)圖片,增加粒子動畫效果,非常的炫酷,需要的朋友可以參考下2015-05-05JSP防止網(wǎng)頁刷新重復(fù)提交數(shù)據(jù)的幾種方法
這篇文章主要介紹了JSP防止網(wǎng)頁刷新重復(fù)提交數(shù)據(jù)的幾種方法,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11