小秋的家

home

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  1 Posts :: 91 Stories :: 17 Comments :: 0 Trackbacks
    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"
posted on 2008-08-28 17:23 棋剑小秋 阅读(162) 评论(0)  编辑  收藏 所属分类: AJAX

只有注册用户登录后才能发表评论。


网站导航: