Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map() is not defined in Google Chrome

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:

Uncaught ReferenceError: Map is not defined

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;
}
like image 591
Nick Avatar asked Jun 22 '13 13:06

Nick


2 Answers

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

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

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

chromium --js-flags=--harmony
chrome.exe --jsflags=--harmony
like image 132
Rob W Avatar answered Sep 22 '22 08:09

Rob W


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.

like image 43
Marc Rochkind Avatar answered Sep 22 '22 08:09

Marc Rochkind