我的漫漫程序之旅

专注于JavaWeb开发

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 々上善若水々 阅读(26) 评论(0)  编辑  收藏 所属分类: JavaScript


标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
 
 

相关链接:
网站导航:

导航

<2008年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

统计

留言簿(12)

随笔档案(24)

文章分类(277)

文章档案(228)

搜索

最新评论

阅读排行榜

评论排行榜