javascript Map() 未在 Google Chrome 中定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17251315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:38:37  来源:igfitidea点击:

Map() is not defined in Google Chrome

javascriptgoogle-chromemapecmascript-harmony

提问by Nick

I've been searching for an answer but I'm only getting results regarding the Google maps API. I'm trying to use a map in JavaScript to map an integer to a string. Everything is working fine in Firefox but in chrome I get an error message in the console:

我一直在寻找答案,但我只得到有关 Google 地图 API 的结果。我正在尝试使用 JavaScript 中的映射将整数映射到字符串。在 Firefox 中一切正常,但在 chrome 中,我在控制台中收到一条错误消息:

Uncaught ReferenceError: Map is not defined

未捕获的 ReferenceError:未定义地图

Below is a piece of reference code:

下面是一段参考代码:

var NameMap;
var DistanceMap;

function FillMaps(){
    NameMap = new Map();
    DistanceMap = new Map();

    NameMap.set(01, "Araba/álava");
}

function CheckName(_field){
    var value = document.getElementsByName(_field.name).item(0).value;
    var location = value.charAt(0) + value.charAt(1);
    var result = NameMap.get(parseInt(location));
    if(result == undefined){
        result = "Unknown";
    }
    document.getElementById('loc').innerHTML = result;
}

回答by Rob W

Some ES harmony features, including Map(), did already exist in Chrome at the end of 2011. It's just disabled by default.

一些 ES 和谐特性,包括Map(),在 2011 年底已经存在于 Chrome 中。它只是默认禁用。

To enable the feature, visit chrome://flags/, and enable "Enable experimental JavaScript". Then restart the browser and enjoy the new features.

要启用该功能,请访问chrome://flags/并启用“启用实验性 JavaScript”。然后重新启动浏览器并享受新功能。

An alternative method is to pass the --js-flags=--harmonyflag on the command line. For instance:

另一种方法是--js-flags=--harmony在命令行上传递标志。例如:

chromium --js-flags=--harmony
chrome.exe --jsflags=--harmony

回答by Marc Rochkind

Map is now in Chrome version 38 (beta as of 29-Aug-2014), which means it will get to stable very shortly. If you have 38, no need to enable experimental features.

Map 现在在 Chrome 版本 38(测试版截至 2014 年 8 月 29 日),这意味着它很快就会稳定下来。如果您有 38 个,则无需启用实验性功能。

回答by user2313067

Why not use a simple array?

为什么不使用简单的数组?

var NameMap;
var DistanceMap;

function FillMaps(){
    NameMap = new Array();
    DistanceMap = new Array();

    NameMap[01]="Araba/álava";

}

function CheckName(_field){
    var value = document.getElementsByName(_field.name).item(0).value;
    var location = value.charAt(0) + value.charAt(1);
    var result = NameMap[parseInt(location)];
    if(result == undefined){
        result = "Unknown";
    }
    document.getElementById('loc').innerHTML = result;
}

回答by Spacedman

"Danger! Experimental technology!"

“危险!实验技术!”

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

Its possible the code here can implement Map for Chrome, but I've not tried:

这里的代码可能可以为 Chrome 实现 Map,但我没有尝试过:

http://wiki.ecmascript.org/doku.php?id=harmony:simple_maps_and_sets

http://wiki.ecmascript.org/doku.php?id=harmony:simple_maps_and_sets