將對象轉為hash對象
Creates a Hash
(which is synonymous to "map" or "associative array" for our purposes). A convenience wrapper around the Hash
constructor, with a safeguard that lets you pass an existing Hash
object and get it back untouched (instead of uselessly cloning it).
The $H
function is the shorter way to obtain a hash (prior to 1.5 final, it was the only proper way of getting one).
object.prototype._proto_、?
- <!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?
- ?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">?
- <html>?
- ????<head>?
- ????????<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/>?
- ????????<title>$H</title>?
- ????????<script?type="text/javascript"?language="javascript"?
- ?????????src="prototype.js"?></script>?
- ????????<script>?
- ????????function?test()?
- ????????{?
- ????????????//?創建一個對象,對于像sucre這樣的字符串要記得加引號?
- ????????????var?obj?=?{?
- ????????????????userName:?'sucre',?
- ????????????????password:?'secret',?
- ????????????????age:?27?
- ????????????????};?
- ?????
- ????????????//?將其轉換為Hash對象?
- ????????????var?hash?=?$H(obj);?
- ????????????alert(hash.toQueryString());?
- ????????}?
- ????????function?getValue(){?
- ????????????var?h?=?$H({name:?'sucre',?age:?27,?country:?'China'});?
- ????????????//?Equivalent?to:?
- ????????????var?hh?=?new?Hash({name:?'sucre',?age:?27,?country:?'China'});?
- ????????????//?Can?then?be?accessed?the?classic?Hash?way?
- ????????????var?country?=?h.get('country');?
- ????????????var?name?=?hh.get('name');?
- ????????????//看到了輸出的結果是一樣的,說明$H與new?Hash是一回事?
- ????????????alert("h中的country:"+country+"\r\n"+"hh中的name:"+name);?
- ????????}?
- ????????</script>?
- ????</head>?
- ????<body>?
- ????????<form>?
- ????????????<input?type="button"?value="轉換"?onclick="test()"?/>?
- ????????????<input?type="button"?value="取值"?onclick="getValue()"?/>?
- ????????</form>?
- ????</body>?
- </html>?
?