js的Array的map和sort实现方法

  Array.prototype.mapA = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
res[i] = fun.call(thisp, this[i], i, this);
}
return res;
}; var ss = [1,2,3,4];
var b = ss.mapA(function(item){
return item+3;
});
//console.log(b); function BubbleSort(array,cb) {
var length = array.length;
for (var i = length - 1; i > 0; i--) { //用于缩小范围
for (var j = 0; j < i; j++) { //在范围内进行冒泡,在此范围内最大的一个将冒到最后面
if(cb.call(array,array[j],array[j+1])>0){
var temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
return array;
}
Array.prototype.sortA = function(fun /*, thisp*/){
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var res = new Array(len);
res = BubbleSort(this,fun);
return res;
};
var sss = [32,23,5,95,4];
var bb= sss.sortA(function(a,b){return a-b});
console.log(bb);
上一篇:Redis:默认配置文件redis.conf详解


下一篇:iOS、Xcode监测键盘的显示和隐藏变化,并获得键盘高度,改变tableView的frame和偏移