修复IE9&safari 的sort方法


当前版本 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 < 2) return this;
var i = 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 = (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);

« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3