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,如需转载请自行联系原作者


相关文章
|
5天前
|
JavaScript 前端开发
在JavaScript中,函数原型(Function Prototype)是一个特殊的对象
【5月更文挑战第11天】JavaScript中的函数原型是一个特殊对象,它为所有函数实例提供共享的方法和属性。每个函数在创建时都有一个`prototype`属性,指向原型对象。利用原型,我们可以向所有实例添加方法和属性,实现继承。例如,我们定义一个`Person`函数,向其原型添加`greet`方法,然后创建实例`john`和`jane`,它们都能调用这个方法。尽管可以直接在原型上添加方法,但推荐在构造函数内部定义以封装数据和逻辑。
18 2
|
9月前
|
前端开发 JavaScript API
ES6-ES11-第一部分-let、const、解构赋值、模板字符串、简化对象写法、箭头函数、函数参数默认值、rest 参数、扩展运算符、Symbol、迭代器、生成器、Promise、Set、Map(五)
ES6-ES11-第一部分-let、const、解构赋值、模板字符串、简化对象写法、箭头函数、函数参数默认值、rest 参数、扩展运算符、Symbol、迭代器、生成器、Promise、Set、Map(五)
|
9月前
|
前端开发 API 网络架构
ES6-ES11-第一部分-let、const、解构赋值、模板字符串、简化对象写法、箭头函数、函数参数默认值、rest 参数、扩展运算符、Symbol、迭代器、生成器、Promise、Set、Map(六)
ES6-ES11-第一部分-let、const、解构赋值、模板字符串、简化对象写法、箭头函数、函数参数默认值、rest 参数、扩展运算符、Symbol、迭代器、生成器、Promise、Set、Map(六)
|
9月前
|
前端开发 网络架构
ES6-ES11-第一部分-let、const、解构赋值、模板字符串、简化对象写法、箭头函数、函数参数默认值、rest 参数、扩展运算符、Symbol、迭代器、生成器、Promise、Set、Map(一)
ES6-ES11-第一部分-let、const、解构赋值、模板字符串、简化对象写法、箭头函数、函数参数默认值、rest 参数、扩展运算符、Symbol、迭代器、生成器、Promise、Set、Map
|
9月前
|
前端开发 网络架构
ES6-ES11-第一部分-let、const、解构赋值、模板字符串、简化对象写法、箭头函数、函数参数默认值、rest 参数、扩展运算符、Symbol、迭代器、生成器、Promise、Set、Map(四)
ES6-ES11-第一部分-let、const、解构赋值、模板字符串、简化对象写法、箭头函数、函数参数默认值、rest 参数、扩展运算符、Symbol、迭代器、生成器、Promise、Set、Map(四)
|
9月前
|
前端开发 JavaScript 网络架构
ES6-ES11-第一部分-let、const、解构赋值、模板字符串、简化对象写法、箭头函数、函数参数默认值、rest 参数、扩展运算符、Symbol、迭代器、生成器、Promise、Set、Map(三)
ES6-ES11-第一部分-let、const、解构赋值、模板字符串、简化对象写法、箭头函数、函数参数默认值、rest 参数、扩展运算符、Symbol、迭代器、生成器、Promise、Set、Map(三)
|
JavaScript 前端开发
Function() 构造函数
Function() 构造函数
47 0
Array.prototype.includes() 原型调用用法案例讲解
Array.prototype.includes() 原型调用用法案例讲解
89 2
|
JavaScript 网络架构 索引
Arguments 对象与简易柯里化
Arguments 对象与简易柯里化
140 0
|
JavaScript 前端开发
函数原型中的 call 和 apply 方法的区别
它们是在 JavaScript 引擎内在实现的,因为属于Function.prototype,所以每个Function对象实例,也就是每个方法都有call, apply属性。它们的作用一样,只是使用方式不同。
112 0