|
<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>
|