js快排与namespace

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);

  

js快排与namespace,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。