我想在 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/