我的漫漫程序之旅

          專注于JavaWeb開發
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數據加載中……

          Prototype1.6 實戰08 (Hash)

          <HTML>
           
          <HEAD>
            
          <TITLE> Hash </TITLE>
          <script src="prototype.js" type="text/javascript" ></script>
          <script>
              
          function hash$()
              
          {
                  
          //聲明一個Hash
                  var h  = new Hash({a : "apple",b : "banana",c:"coconut"});
                  
          //set key and value after
                  h.set("d","orange");
                  
          //檢查有無空元素
                  h.inspect();
                  
          //迭代輸出
                  h.keys().each(function(i){
                      alert(h.get(i));
                  }
          );
              }


              
          function remove$()
              
          {
                  
          var h = new Hash({a : "apple" , b : "banana" , c : "coconut" });
                  
          //刪除指定元素
                  h.unset('a','b');
                  alert(h.values());
                  
              }

              
          //把hash轉換為URL編碼的查詢字符串表現形式
              function toQueryString$()
              
          {
                  
          var h = new Hash({a : "apple" , b : "banana" , c : "coconut" });
                  alert(h.toQueryString());
              }

              
          //返回JSON形式的數據格式
              function toJSON$()
              
          {
                  
          var h = new Hash({a : "apple" , b : "banana" , c : "coconut" });
                  alert(h.toJSON());
              }

              
          //返回Object形式的數據格式  
              function toObject$()
              
          {
                  
          var h = new Hash({a : "apple" , b : "banana" , c : "coconut" });
                  
          var obj = h.toObject();
                  alert(Object.inspect(h));
                  alert(Object.inspect(obj));
                  alert(obj.a);
                  obj.a 
          = "orange";
                  alert(
          "object: " + obj.a + "  hash: " + h.get('a'));
              }

              
          //merge 合并并返回一個新hash
              function merge$()
              
          {
                  
          var h = new Hash({a : "apple" , b : "banana" , c : "coconut" });
                  
          var h2 = h.merge({d : "orange", f : "pear"}).inspect();
                  alert(h2);
              }

          </script>
           
          </HEAD>
           
          <BODY>
              
          <button onclick="hash$();">add</button>
              
          <button onclick="remove$();">remove</button>
              
          <button onclick="toQueryString$();">toQueryString</button>
              
          <button onclick="toJSON$();">toJSON</button>
              
          <button onclick="toObject$();">toObject</button>
              
          <button onclick="merge$();">merage</button>
           
          </BODY>
          </HTML>


          posted on 2008-07-25 13:56 々上善若水々 閱讀(237) 評論(0)  編輯  收藏 所屬分類: JavaScript

          主站蜘蛛池模板: 洪泽县| 赣榆县| 山丹县| 大庆市| 枝江市| 阳泉市| 牟定县| 阳高县| 营山县| 新乡市| 扶绥县| 岗巴县| 毕节市| 万源市| 咸丰县| 确山县| 永嘉县| 阳泉市| 道真| 八宿县| 鱼台县| 柳河县| 湄潭县| 鄂州市| 皮山县| 龙口市| 和田县| 潍坊市| 霍城县| 哈尔滨市| 马龙县| 伊宁市| 襄垣县| 宁化县| 长岛县| 泰和县| 福州市| 普陀区| 龙口市| 丹阳市| 焉耆|