我的漫漫程序之旅

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


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


网站导航: