zl程序教程

您现在的位置是:首页 >  其他

当前栏目

修复IE9&safari的sort方法

amp方法 修复 Sort safari IE9
2023-06-13 09:14:30 时间
当前版本v0.2
v0.1修复IE9--Array.prototype.sort不能根据对象属性做排序的遗憾
v0.2修复safari不支持函数参数
复制代码代码如下:


!function(window){
varua=window.navigator.userAgent.toLowerCase(),
reg=/msie|applewebkit.+safari/;
if(reg.test(ua)){
var_sort=Array.prototype.sort;
Array.prototype.sort=function(fn){
if(!!fn&&typeoffn==="function"){
if(this.length<2)returnthis;
vari=0,j=i+1,l=this.length,tmp,r=false,t=0;
for(;i<l;i++){
for(j=i+1;j<l;j++){
t=fn.call(this,this[i],this[j]);
r=(typeoft==="number"?t:
!!t?1:0)>0
?true:false;
if(r){
tmp=this[i];
this[i]=this[j];
this[j]=tmp;
}
}
}
returnthis;
}else{
return_sort.call(this);
}
};
}
}(window);