-1

发现并修复了我自己的错误。问题是我在另一个文档中有另一个名为“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>
4

1 回答 1

0

问题解决了。请注意,即使在不同的文档中,您也没有具有相同名称的变量。

于 2013-07-05T08:24:23.723 回答