JS模擬的Map類實(shí)現(xiàn)方法
本文實(shí)例講述了JS模擬的Map類。分享給大家供大家參考,具體如下:
根據(jù)java中map的屬性,實(shí)現(xiàn)key----value保存
1、使用數(shù)組方式存儲(chǔ)數(shù)據(jù),(使用閉包)
function Map() { var struct = function (key, value) { this.key = key; this.value = value; } var put = function (key, value) { for (var i = 0; i < this.arr.length; i++) { if (this.arr[i].key === key) { this.arr[i].value = value; return; } } this.arr[this.arr.length] = new struct(key, value); } var get = function (key) { for (var i = 0; i < this.arr.length; i++) { if (this.arr[i].key === key) { return this.arr[i].value; } } return null; } var remove = function (key) { var v; for (var i = 0; i < this.arr.length; i++) { v = this.arr.pop(); if (v.key === key) { continue; } this.arr.unshift(v); } } var size = function () { return this.arr.length; } var isEmpty = function () { return this.arr.length <= 0; } this.arr = new Array(); this.get = get; this.put = put; this.remove = remove; this.size = size; this.isEmpty = isEmpty; }
2、使用JSON方式存儲(chǔ)數(shù)據(jù)(使用原型方式拓展方法)
function Map() { this.obj = {}; this.count = 0; } Map.prototype.put = function (key, value) { var oldValue = this.obj[key]; if (oldValue == undefined) { this.count++; } this.obj[key] = value; } Map.prototype.get = function (key) { return this.obj[key]; } Map.prototype.remove = function (key) { var oldValue = this.obj[key]; if (oldValue != undefined) { this.count--; delete this.obj[key]; } } Map.prototype.size = function () { return this.count; } var map = new Map(); map.put("key","map"); map.put("key","map1"); alert(map.get("key"));//map1 map.remove("key"); alert(map.get("key"));//undefined
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動(dòng)畫特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- JavaScript中循環(huán)遍歷Array與Map的方法小結(jié)
- 在JavaScript中操作數(shù)組之map()方法的使用
- js實(shí)現(xiàn)的map方法示例代碼
- Java中快速把map轉(zhuǎn)成json格式的方法
- js中遍歷Map對(duì)象的方法
- js中生成map對(duì)象的方法
- js獲取通過ajax返回的map型的JSONArray的方法
- 將JSON字符串轉(zhuǎn)換成Map對(duì)象的方法
- Js遍歷鍵值對(duì)形式對(duì)象或Map形式的方法
- jQuery遍歷json中多個(gè)map的方法
- JS實(shí)現(xiàn)的自定義map方法示例
相關(guān)文章
javascript中的toFixed固定小數(shù)位數(shù) 簡(jiǎn)單實(shí)例分享
這篇文章介紹了toFixed固定小數(shù)位數(shù)的簡(jiǎn)單例子,有需要的朋友可以參考一下2013-07-07JS co 函數(shù)庫(kù)的含義和用法實(shí)例總結(jié)
這篇文章主要介紹了JS co 函數(shù)庫(kù)的含義和用法,結(jié)合實(shí)例形式總結(jié)分析了JS co 函數(shù)庫(kù)的基本含義、功能、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04js 顯示base64編碼的二進(jìn)制流網(wǎng)頁(yè)圖片
base64簡(jiǎn)單地說,它把一些 8-bit 數(shù)據(jù)翻譯成標(biāo)準(zhǔn) ASCII 字符,我們把圖像文件的內(nèi)容直接寫在了HTML 文件中,這樣做的好處是,節(jié)省了一個(gè)HTTP 請(qǐng)求2014-04-04JS動(dòng)態(tài)插入腳本和插入引用外部鏈接腳本的方法
js 動(dòng)態(tài)插入腳本的是在頁(yè)面加載時(shí)不存在,但將來的某一時(shí)刻通過修改該 DOM 動(dòng)態(tài)添加的腳本。接下來通過本文給大家介紹JS動(dòng)態(tài)插入腳本和插入引用外部鏈接腳本,需要的朋友可以參考下2018-05-05IE6/7/8中Option元素未設(shè)value時(shí)Select將獲取空字符串
可以看到當(dāng)忘記寫option的value時(shí)這些現(xiàn)代瀏覽器都會(huì)盡量返回正確的(客戶端程序員想要的)結(jié)果value,其容錯(cuò)性比IE6/7/8做的更好。2011-04-04javascript 裝載iframe子頁(yè)面,自適應(yīng)高度
2009-03-03在layui中l(wèi)ayer彈出層點(diǎn)擊事件無效的解決方法
今天小編就為大家分享一篇在layui中l(wèi)ayer彈出層點(diǎn)擊事件無效的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09JavaScript實(shí)現(xiàn)的簡(jiǎn)單加密解密操作示例
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的簡(jiǎn)單加密解密操作,涉及javascript基于charCodeAt與fromCharCode的字符串編碼與解碼操作相關(guān)使用技巧,需要的朋友可以參考下2018-06-06