Heim  >  Artikel  >  Web-Frontend  >  修复IE9&safari 的sort方法_javascript技巧

修复IE9&safari 的sort方法_javascript技巧

WBOY
WBOYOriginal
2016-05-16 18:00:411022Durchsuche

当前版本 v0.2
v0.1 修复IE9-- Array.prototype.sort 不能根据 对象属性 做排序的遗憾
v0.2 修复safari 不支持函数参数

复制代码 代码如下:


!function(window){
var ua = window.navigator.userAgent.toLowerCase(),
reg = /msie|applewebkit.+safari/;
if(reg.test(ua)){
var _sort = Array.prototype.sort;
Array.prototype.sort = function(fn){
if(!!fn && typeof fn === 'function'){
if(this.length var i = 0, j = i + 1, l = this.length, tmp, r = false, t = 0;
for(; i for(j = i + 1; j t = fn.call(this, this[i], this[j]);
r = (typeof t === 'number' ? t :
!!t ? 1 : 0) > 0
? true : false;
if(r){
tmp = this[i];
this[i] = this[j];
this[j] = tmp;
}
}
}
return this;
}else{
return _sort.call(this);
}
};
}
}(window);
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn