javascript - Node.js:对于每个……不工作

标签 javascript node.js foreach v8

我想在 Node.js (v0.4.11) 中使用 for each ... in

我是这样使用的:

var conf = {
   index: {
      path: {
         first: "index.html",
         pattern: "index/{num}.html"
      },
      template: "index.tpl",
      limit: 8
   },
   feed: {
      path: "feed.xml",
      template: "atom.tpl",
      limit: 8
   }
}

for each (var index in conf) {
  console.log(index.path);
}

我收到以下错误:

        for each (var index in conf) {
     ^^^^

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
SyntaxError: Unexpected identifier
    at Module._compile (module.js:397:25)
    at Object..js (module.js:408:10)
    at Module.load (module.js:334:31)
    at Function._load (module.js:293:12)
    at require (module.js:346:19)
    at Object.<anonymous> (/home/paul/dev/indexing/lib/Index.js:3:13)
    at Module._compile (module.js:402:26)
    at Object..js (module.js:408:10)
    at Module.load (module.js:334:31)
    at Function._load (module.js:293:12)

错在哪里? for each ... in 从 Javascript 1.6 开始支持。

MDN有关 for each ... in 的用法的信息。

最佳答案

不幸的是 node 不支持 for each ... in,即使它是在 JavaScript 1.6 中指定的。 Chrome 使用相同的 JavaScript 引擎并且是 reported as有类似的缺点。

你必须满足于 array.forEach(function(item) {/* etc etc */}).

编辑:来自 Google 的 V8 官方网站:

V8 implements ECMAScript as specified in ECMA-262.

在同一个 MDN 网站上,它说 for each ...in 在 JavaScript 1.6 中,它说它不在任何 ECMA 版本中 - 因此,据推测,它不在 Node.js 中。

关于javascript - Node.js:对于每个……不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7184099/

相关文章:

php - 如何在foreach循环中从数组中删除重复值?

javascript - 从 API 获取页面标题是否会发出未定义警报?

node.js - Angular + Electron 应用程序 : query web API or DataBase according to deskop or web mode

javascript - 为什么无法尝试/捕捉某些东西?

node.js - 从 mongodb 数组中删除一个元素

indexing - 有没有快速的解决方案来从 Node.js 查询 Lucene 索引?

php - 对 foreach 循环结果进行排序

php - foreach 根据点击的复选框数量不断迭代

javascript - 可能的变量提升问题

javascript - jQuery 不工作