js快排与namespace

时间:2024-01-13 12:22:20
 function quickSort(arr){
if(arr.length<=1){
return arr;
}
var num = Math.floor(arr.length/2); var numValue = arr.splice(num,1); var left = [];
var right = []; for(var i=0;i<arr.length;i++){
if(arr[i]<numValue){
left.push(arr[i])
}else {
right.push(arr[i])
}
} return quickSort(left).concat(numValue,quickSort(right)) ;
}
 var arr=[4,35,45,66,12,33,89,8];
var re=quickSort(arr);
console.log(re);//[4, 8, 12, 33, 35, 45, 66, 89]

命名空间

 var namespace = function(ns){
var parts = ns.split('.');
var i,len,
obj = this; for( i=0,len=parts.length;i<len;i++){ if(!obj[parts[i]]){ obj[parts[i]] = {};
}
obj=obj[parts[i]];
} return obj;
}

  

namespace('test');
namespace('test.books').test='book.test'; console.log(test); console.log(test.books);