JavaScript類比的HashMap,請大家多多提意見!

來源:互聯網
上載者:User

/** * V1.0 * HashMap javascript版 * 2013-09-04  * var map = new $.HashMap();    map.put("age","20");    map.put("name","yk1");    alert(map.get("age"));    alert(map.toString());        map = new $.HashMap({"username":"ycyk_168","password":"123456"});    map.put("age","20");    map.put("name","yk1");    alert(map.get("username"));    alert(map.get("password"));    alert(map.get("age"));    alert(map.get("name"));    alert(map.toString()); * */jQuery.HashMap = function(param) {var _map = typeof(param)=="undefined" ? [{}] : [param];/** * 向HashMap中添加資料 */_map.put =  function(key,value){this[0][key] = value;};/** * 從HashMap中擷取指定的Key的值 */_map.get = function(key){return this[0][key];};/** * HashMap中是否包含指定的Key */_map.containsKey = function(key){return this.get(key) == null ? false : true;};/** * 從HshMap中刪除Key */_map.remove = function(key){delete this[0][key];};/** * 擷取HashMap的大小 */_map.size = function(){var count = 0;for(var item in this[0]){count ++;}return count;};/** * 將HashMap轉換為string的資料 */_map.toString = function(){var str = "{";for(var item in this[0]){str += ""+ item +":"+ this[0][item] +",";}str = $.util.removeEnd(str,",") + "}";return str;};/** * 將HashMap轉換為JSON格式的資料 */_map.toJson = function(){return this[0];};return _map;};

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.