完美解決IE低版本不支持call與apply的問題
更新時間:2013年12月05日 09:53:25 作者:
這篇文章主要是對解決IE低版本不支持call與apply的問題進(jìn)行了詳細(xì)的介紹。需要的朋友可以過來參考下,希望對大家有所幫助
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實(shí)現(xiàn)簡單動態(tài)表格
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡單動態(tài)表格,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12javascript轉(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深入理解JavaScript中實(shí)例對象和new命令
典型的面向?qū)ο缶幊陶Z言(比如C++和 Java),都有“類”(class)這個概念。所謂“類”就是對象的模板,對象就是“類”的實(shí)例,下面這篇文章主要給大家介紹了關(guān)于JavaScript中實(shí)例對象和new命令的相關(guān)資料,需要的朋友可以參考下2022-12-12