prototype의 $H 함수 사용

7131 단어 prototype직장레저
대상을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
    "+"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> 

좋은 웹페이지 즐겨찾기