如何创建此 Java 代码的 JavaScript/JQuery 等效项:
Map map = new HashMap(); //Doesn't not have to be a hash map, any key/value map is fine map.put(myKey1, myObj1); map.put(myKey2, myObj2); //Repeat n times function Object get(k) { return map.get(k); }
编辑:过时的答案,ECMAScript 2015 (ES6) 标准 javascript 有一个 Map 实现,在这里阅读更多信息:https ://developer.mozilla.org/en- US/docs/Web/JavaScript/Reference/Global_Objects/Map
var map = new Object(); // or var map = {}; map[myKey1] = myObj1; map[myKey2] = myObj2; function get(k) { return map[k]; } //map[myKey1] == get(myKey1);