Eu escrevi um mapa no JS com função Traversal, por favor, comente.
//map.js
Array.prototype.Remove = função (s) {for (var i = 0; i <this.length; i ++) {if (s == this [i]) this.splice (i, 1); }} /** * mapa simples * * var m = novo map (); * m.put ('key', 'value'); * ... * var s = ""; * M.Each (função (chave, valor, índice) { * s+= index+":"+key+"="+value+"/n"; *}); * alerta (s); * * @Author DeWitt * @Date 2008-05-24 */FUNÇÃO MAP () {/** Array para armazenar as chaves (usadas para traversal) */this.keys = new Array (); / ** Dados de armazenamento*/ this.data = new Object (); / ** * Coloque um par de valores-chave * @param {string} key * @param {object} value */ this.put = function (key, value) {if (this.data [key] == null) {this.keys.push (key); } this.data [key] = value; }; / ** * Obtenha o valor correspondente a uma chave * @param {string} key * @return {object} value */ this.get = function (key) {return this.data [key]; }; / *** Exclua um par de valores-chave* @param {string} key*/ this.remove = function (key) {this.keys.remove (key); this.data [key] = null; }; / ** * Atravessando o mapa e executando a função de processamento * * @param {function} função de retorno de chamada (chave, value, index) {..} */ 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); }}; / *** Obtenha uma matriz de valores-chave (semelhante ao Java's EntrySet ())* @return Matriz do objeto de valor-chave {key, value}*/ this.entrys = function () {var len = this.keys.length; Var Entrys = New Array (LEN); for (var i = 0; i <len; i ++) {entradas [i] = {chave: this.keys [i], valor: this.data [i]}; } retornar entradas; }; / *** Determine se o mapa está vazio*/ this.isempty = function () {return this.keys.length == 0; }; / *** Obtenha o número de pares de valor-chave*/ this.size = function () {return this.keys.length; }; / ** * Reescreva a 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+= "}"; retorno s; }; } função testMap () {var m = new Map (); m.put ('key1', 'comtop'); m.put ('key2', 'Southern Power Grid'); M.put ('Key3', 'Jingxin Garden'); alerta ("init:"+m); m.put ('key1', 'kantop'); alerta ("Set Key1:"+M); M.Remove ("Key2"); alerta ("Remover Key2:"+M); var s = ""; M.Each (função (chave, valor, índice) {s+= index+":"+key+"="+value+"/n";}); alerta (s); } //testmap.htm? <html> <head> <title> mapa de teste </title> <script idioma = "javascript" src = "map.js"> </script> </ad Head> <body> </body type = "button" "" test) <Clodll = "testmap ()"> <///body> </ht type = "" "" "" testMap)O artigo acima é um exemplo simples de mapa JavaScript (compartilhamento), que é todo o conteúdo que eu compartilho com você. Espero que você possa lhe dar uma referência e espero que você possa apoiar mais o wulin.com.