트래버스 기능이있는 JS에지도를 썼습니다.
//map.js
array.prototype.remove = function (s) {for (var i = 0; i <this.length; i ++) {if (s == this [i]) this.splice (i, 1); }} /** * 간단한 맵 * * * var m = new Map (); * m.put ( 'key', 'value'); * ... * var s = ""; * M.Each (함수 (키, 값, 색인) { * s+= index+":"+key+"="+value+"/n"; *}); * 경고; * * @author dewitt * @date 2008-05-24 */function map () {/** 키를 저장하기위한 배열 (Traversal에 사용) */this.keys = new Array (); / ** 저장 데이터*/ this.data = new Object (); / ** * 키-값 쌍 * @param {string} key * @param {object} value */ this.put = function (key, value) {if (this.data [key] == null) {this.keys.push (key); } this.data [key] = value; }; / ** * 키 * @param {string} key * @return {object} value */ this.get = function (key) {return this.data [key]; }; / *** 키 값 쌍 삭제* @param {string} key*/ this.remove = function (key) {this.keys.remove (키); this.data [key] = null; }; / ** * 맵을 가로 지르고 처리 함수 실행 * * @param {function} 콜백 함수 함수 (키, 값, 색인) {..} */ this.each = function (fn) {if (typeof fn! = 'function') {return; } var len = this.keys.length; for (var i = 0; i <len; i ++) {var k = this.keys [i]; fn (k, this.data [k], i); }}; / *** 키-값 배열을 가져옵니다 (java 's entryset ())* @return key-key value 객체 {key, value}*/ this.entrys = function () {var len = this.keys.length; var entrys = new Array (Len); for (var i = 0; i <len; i ++) {entrys [i] = {key : this.keys [i], value : this.data [i]}; } 리턴 항목; }; / *** 맵이 비어 있는지 결정*/ this.isempty = function () {return this.keys.length == 0; }; / *** 키 값 쌍의 수를 얻으십시오*/ this.size = function () {return this.keys.length; }; / ** * toString을 다시 작성 */ this.toString = function () {var s = "{"; for (var i = 0; i <this.keys.length; i ++, s+= ',') {var k = this.keys [i]; s+= k+"="+this.data [k]; } s+= "}"; 반환 s; }; } function testMap () {var m = 새 map (); m.put ( 'key1', 'comtop'); M.put ( 'key2', 'Southern Power Grid'); m.put ( 'key3', 'jingxin garden'); 경고 ( "init :"+m); m.put ( 'key1', 'kantop'); 경고 ( "set key1 :"+m); m.remove ( "key2"); 경고 ( "key2 :"+m); var s = ""; M.Each (함수 (키, 값, 색인) {S+= index+":"+key+"="+value+"/n";}); 경고 (들); } //testmap.htm? <html> <html> <head> <title> title map </title> <script language = "javaScript"src = "map.js"> </script> </head> <body> <입력 유형 = "value"value = "test"onclick = "testmap ()"> </html> </html>위의 기사는 간단한 JavaScript 맵 예제 (공유)입니다. 나는 당신이 당신에게 참조를 줄 수 있기를 바랍니다. 그리고 당신이 wulin.com을 더 지원할 수 있기를 바랍니다.