DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> ajax入門指南(六)
ajax入門指南(六)
編輯:AJAX詳解     
ajax學習入門指南,相信對AJax初學者會有所幫助的。

 Prototype框架
    枚舉對象(Enumerable對象)
        1.Enumerable.each(iterator)
        each方法的參數是一個JavaScript函數,它可以接受兩個參數,它們分別是當前遍歷的集合元素和當前遍歷元素在集合中的序號
        示例:
        var arr = [1,2,3,4];
        arr.each(
           function(item,index){
               if(index == 2)
                  alert("arr["+index+"]=" +item);//當序號為2的時候,輸出"arr[2]=3"
               }
           );

        2.$break和$continue對象
        遍歷集合元素時實現一定的流程控制
        示例:
        var arr = [1,2,3,4,8,5,4,3];
        //輸出結果為:1,2,3,4,5,4,3
        arr.each(
           function(item,index){
               if(item < 6){
                  alert(item);
              } else{
                //出現大於等於6的數跳過,繼續處理下一個元素
               $continue; //如果按照如下的例子用throw $continue將會結束遍歷
              }
           }
      );
 
        var arr = [1,2,3,4,8,5,4,3];
        //輸出結果為:1,2,3,4,5,4,3
        arr.each(
           function(item,index){
               if(item < 6){
                  alert(item);
              } else{
                //出現大於等於6的數跳過,繼續處理下一個元素
               throw $break;
              }
           }
      );

        3.Enumerable.all(iterator)
        用於判斷集合內的所有元素是否符合某個條件,如果條件均返回true,那麼all方法返回true,否則返回false
        示例:
        var arr = [1,2,3,4,8,5,-4,3];
       var boolean = arr.all(
           function(item,index){
               return(item>0)
           }
       );
       alert(boolean);    //輸出false;

            4.Enumerable.collect(iterator)
            遍歷集合中所有元素,返回的結果按照集合中的順序生成一個Array對象返回
            示例:
            var arr = [1,2,3,4,8,5,4,3];
            var newArr = arr.collect(
       function(item,index){
                       return(item * item)
                   }
              );
           alert(newArr);//輸出"1,4,9,16,64,25,16,9"

            5.Enumerable.find(iterator)
            返回滿足條件的元素值或者布爾值
            示例:
            var arr = [1,2,3,4,8,5,4,3];
            var ele = arr.find(
                   function(item,index){
                        if(item > 3)
                          return(item) 
                  }
             );
           alert(ele);//輸出值:4
           黑體部分可以換成:return (item > 3)返回布爾值用來判斷該集合是否有滿足條件的元素

            6.Enumerable.findAll(iterator)
                滿足條件的所有元素以Array對象返回
                var arr = [1,2,3,4,8,5,4,3];
                var ele = arr.findAll(
                       function(item,index){
                            if(item > 3)
                              return(item)
                      }
                );
               alert(ele);//輸出"4,8,5,4"

            7.Enumerable.grep(pattern,iterator)
                pattern參數是正則表達式,
                示例:
                var arr = ["12345","abc2","cde","fgh","132ba"];
                var newArray = arr.grep(
                           /2/,
                           function(item,index){
                                alert(item);//輸出"12345","abc2","132ba"
                           }
               );
    8.Enumerable.include(obj)
                判斷obj對象是否是集合的成員
                示例:
                var arr = [1,2,3,4,5];
                var ele = arr.include(2);
               alert(ele);//輸出true

            9.Enumerable.max(iterator)
                返回集合中最大元素
                示例:
                var arr = [1,2,3,4,5];
                var ele = arr.max();
                alert(ele); //輸出5

            10.Enumerable.min(iterator)
                返回集合中最小元素

            11.Enumerable.sortBy(iterator)
                 進行排序
                 示例:
                var arr = [1,5,3,2,4];
                var arr = arr.sortBy(
                     function(item,index){
                           return item;
                     }
               );
               alert(arr);//輸出"1,2,3,4,5"