Escribí un mapa en JS con función transversal, por favor comente.
//map.js
Array.prototype.remove = function (s) {for (var i = 0; i <this.length; i ++) {if (s == this [i]) this.splice (i, 1); }} /** * mapa simple * * * var m = nuevo map (); * m.put ('clave', 'valor'); * ... * var s = ""; * M.Each (función (clave, valor, índice) { * s+= index+":"+key+"="+valor+"/n"; *}); * alerta (s); * * @author Dewitt * @Date 2008-05-24 */function map () {/** matriz para almacenar las claves (utilizadas para traversal) */this.keys = new array (); / ** Tienda datos*/ this.data = new Object (); / ** * Pon un par de valores clave * @param {String} Key * @param {object} valor */ this.put = function (key, valor) {if (this.data [key] == null) {this.keys.push (key); } this.data [clave] = valor; }; / ** * Obtenga el valor correspondiente a una tecla * @param {String} Key * @return {object} valor */ this.get = function (key) {return this.data [key]; }; / *** Eliminar un par de valores clave* @param {String} key*/ this.remove = function (key) {this.keys.remove (clave); this.data [key] = null; }; / ** * atravesando el mapa y ejecutando la función de procesamiento * * @param {function} función de devolución de llamada (key, valor, index) {..} */ this.each = function (fn) {if (typeOf fn! = 'Function') {return; } var len = this.keys.length; para (var i = 0; i <len; i ++) {var k = this.keys [i]; fn (k, this.data [k], i); }}; / *** Obtenga una matriz de valor clave (similar a la entrada de Java ())* @return del objeto Key-Value {Key, valor}*/ this.entrys = function () {var Len = this.keys.length; entrys var = nueva matriz (len); for (var i = 0; i <len; i ++) {entrys [i] = {key: this.keys [i], valor: this.data [i]}; } Entradas de retorno; }; / *** Determine si el mapa está vacío*/ this.isempty = function () {return this.keys.length == 0; }; / *** Obtenga el número de pares de valor clave*/ this.size = function () {return this.keys.length; }; / ** * Reescribir toString */ this.ToString = function () {var s = "{"; para (var i = 0; i <this.keys.length; i ++, s+= ',') {var k = this.keys [i]; s+= k+"="+this.data [k]; } s+= "}"; regreso s; }; } function 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 ("Establecer clave1:"+m); M.Remove ("Key2"); alerta ("eliminar la tecla2:"+m); var s = ""; M.Each (función (clave, valor, índice) {s+= index+":"+key+"="+valor+"/n";}); alerta (s); } //testmap.htm? <html> <fead> <title> Test Map </title> <script language = "javascript" src = "map.js"> </script> </thead> <body> <input type = "button" value = "test" onclick = "testMap ()"> </body> </html>El artículo anterior es un simple ejemplo de mapa de JavaScript (compartir) que es todo el contenido que comparto con usted. Espero que pueda darle una referencia y espero que pueda apoyar más a Wulin.com.