prototype中的$H函数的用法

将对象转为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).

 

 
 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  2.  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3. <html> 
  4.     <head> 
  5.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  6.         <title>$H</title> 
  7.         <script type="text/javascript" language="javascript" 
  8.          src="prototype.js" ></script> 
  9.         <script> 
  10.         function test() 
  11.         { 
  12.             // 创建一个对象,对于像sucre这样的字符串要记得加引号 
  13.             var obj = { 
  14.                 userName: 'sucre', 
  15.                 password: 'secret', 
  16.                 age: 27 
  17.                 }; 
  18.      
  19.             // 将其转换为Hash对象 
  20.             var hash = $H(obj); 
  21.             alert(hash.toQueryString()); 
  22.         } 
  23.         function getValue(){ 
  24.             var h = $H({name: 'sucre', age: 27, country: 'China'}); 
  25.             // Equivalent to: 
  26.             var hh = new Hash({name: 'sucre', age: 27, country: 'China'}); 
  27.             // Can then be accessed the classic Hash way 
  28.             var country = h.get('country'); 
  29.             var name = hh.get('name'); 
  30.             //看到了输出的结果是一样的,说明$H与new Hash是一回事 
  31.             alert("h中的country:"+country+"\r\n"+"hh中的name:"+name); 
  32.         } 
  33.         </script> 
  34.     </head> 
  35.     <body> 
  36.         <form> 
  37.             <input type="button" value="转换" onclick="test()" /> 
  38.             <input type="button" value="取值" onclick="getValue()" /> 
  39.         </form> 
  40.     </body> 
  41. </html> 

 本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/410405,如需转载请自行联系原作者


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值