1 Array.prototype.mapA = function(fun /*, thisp*/) 2 { 3 var len = this.length; 4 if (typeof fun != "function") 5 throw new TypeError(); 6 var res = new Array(len); 7 var thisp = arguments[1]; 8 for (var i = 0; i < len; i++) 9 { 10 if (i in this) 11 res[i] = fun.call(thisp, this[i], i, this); 12 } 13 return res; 14 }; 15 16 var ss = [1,2,3,4]; 17 var b = ss.mapA(function(item){ 18 return item+3; 19 }); 20 //console.log(b); 21 22 function BubbleSort(array,cb) { 23 var length = array.length; 24 for (var i = length - 1; i > 0; i--) { //用于缩小范围 25 for (var j = 0; j < i; j++) { //在范围内进行冒泡,在此范围内最大的一个将冒到最后面 26 if(cb.call(array,array[j],array[j+1])>0){ 27 var temp = array[j]; 28 array[j] = array[j+1]; 29 array[j+1] = temp; 30 } 31 } 32 } 33 return array; 34 } 35 Array.prototype.sortA = function(fun /*, thisp*/){ 36 var len = this.length; 37 if (typeof fun != "function") 38 throw new TypeError(); 39 var res = new Array(len); 40 res = BubbleSort(this,fun); 41 return res; 42 }; 43 var sss = [32,23,5,95,4]; 44 var bb= sss.sortA(function(a,b){return a-b}); 45 console.log(bb);