在Java中存在一个HashMap类型,具有以下功能:
具体使用方式如下:
HashMap map = new HashMap(); map.put("key1",100); map.put("key2",200); map.get("key1");//100 map.get("key1");//200
JavaScript中并不存在HashMap类型,要求在JavaScript中模拟出一个HashMap类型对象
要求具有上述所有功能。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>JS实现HashMap对象</title> </head> <body> <script type="text/javascript"> function HashMap() { var obj = new Object(); this.put = function(key,value) { obj[key] = value; } this.get = function(key) { return obj[key]; } } var map = new HashMap(); map.put("key1",100); map.put("key2",200); alert(map.get("key1"));//100 </script> </body> </html>