1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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)) ;
}
|
1
2
3
|
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]
|
命名空间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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;
}
|
1
2
3
4
5
6
|
namespace( ‘test‘ );
namespace( ‘test.books‘ ).test= ‘book.test‘ ;
console.log(test); console.log(test.books); |