发现并修复了我自己的错误。问题是我在另一个文档中有另一个名为“map”的变量,所以在我的代码周围没有工作。我将把这段代码留在这里作为如何制作地图的示例。
<html>
<head>
<script type="text/javascript">
var map = {};
map["red"] = "not avaliable";
map["blue"] = "avaliable";
function car(color){
this.color = color;
}
function initialize(){
var testCar = new car("blue");
alert("Value is obviously blue: " + testCar.color);
if (testCar.color in map) {
var mappedValue = map[testCar.color];
console.log("Your car in "+ testCar.color + " is "+ mappedValue);
} else {
console.log("No color "+ testCar.color + "in maps");
}
}
</script>
</head>
<body onload="initialize()"></body>
</html>