0

我以这种方式存储了一些数据:

   var protossUnitsArray =
   [
      { name: "Zealot", health:"100", shield:"50", armor:"1", picture: "/images/protoss/zealotPortrait.png" },
      { name: "Stalker", health: "80", shield: "80", armor: "1", picture: "/images/protoss/stalkerPortrait.png" },
      { name: "Sentry", health: "40", shield: "40", armor: "1", picture: "/images/protoss/sentryPortrait.png" },
      { name: "High Templar", health: "40", shield: "40", armor: "0", picture: "/images/protoss/hightemplarPortrait.png" },
      { name: "Dark Templar", health: "40", shield: "80", armor: "1", picture: "/images/protoss/darktemplarPortrait.png" },
      { name: "Immortal", health: "200", shield: "100", armor: "1", picture: "/images/protoss/immortalPortrait.png" }
   ];

   var protossUnitsList = new WinJS.Binding.List(protossUnitsArray);

   var publicMembers =
   {
      itemList: protossUnitsList
   };

   WinJS.Namespace.define("ProtossUnitsData", publicMembers);

如您所见,该数组已公开发布。

我如何从另一个 javascript 文件中实际引用该数组中的数据?

假设我想知道 Stalker 的健康状况(即 80)。假设我知道 Stalkers 总是会成为数组中的第二个项目,不应该是这样的吗

ProtossUnitsData.itemList[1]['health']?

但这不太对。。

4

3 回答 3

0

弄清楚了!其实是:

ProtossUnitsData.itemList.getAt(1).health;
于 2012-10-18T12:22:47.283 回答
0

我不了解 Windows 8 和以前称为 Metro 的 UI,但在 JavaScript 中不应该只是:

ProtossUnitsData.itemList[1].health

编辑

查看文档,似乎WinJS.Binding.List公开了处理列表中数据的方法。有关更多信息,请参见此处此处,但看起来您想使用:

var item = ProtossUnitsData.getItem(1),
    data = item.health;

那可能行得通...

于 2012-10-18T12:04:28.270 回答
0

如果你想从数组中访问第二个项目protossUnitsArray,不应该是:

protossUnitsArray[1].health
于 2012-10-18T12:07:55.930 回答