I wanted to use for each ... in
with Node.js (v0.4.11).
我想为每一个……在节点。js(v0.4.11)。
I use it like this:
我这样使用它:
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);
}
I get the following error:
我得到以下错误:
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)
Where is the mistake? for each ... in
is supported since Javascript 1.6.
错误在哪里?对于每一个…从Javascript 1.6开始就支持in。
See MDN for information about the usage of for each ... in
.
请参阅MDN以获取关于每个…的用法的信息。在。
4 个解决方案
#1
102
Unfortunately node does not support for each ... in
, even though it is specified in JavaScript 1.6. Chrome uses the same JavaScript engine and is reported as having a similar shortcoming.
不幸的是,node并不支持每一个…即使在JavaScript 1.6中指定了它。Chrome使用相同的JavaScript引擎,并被报告有类似的缺点。
You'll have to settle for array.forEach(function(item) { /* etc etc */ })
.
你将不得不满足于array.forEach(函数(项){/*等*/})。
EDIT: From Google's official V8 website:
编辑:来自谷歌官方V8网站:
V8 implements ECMAScript as specified in ECMA-262.
V8实现了ECMAScript,如ECMA-262中指定的那样。
On the same MDN website where it says that for each ...in
is in JavaScript 1.6, it says that it is not in any ECMA version - hence, presumably, its absence from Node.
在同一个MDN网站上,它说每个…在JavaScript 1.6中,它说它不在任何ECMA版本中——因此,可以推测,它不在Node中。
#2
61
for (var i in conf) {
val = conf[i];
console.log(val.path);
}
#3
6
https://github.com/cscott/jsshaper implements a translator from JavaScript 1.8 to ECMAScript 5.1, which would allow you to use 'for each' in code running on webkit or node.
https://github.com/cscott/jsshaper实现了一个从JavaScript 1.8到ECMAScript 5.1的转换器,它允许你在webkit或节点上运行的代码中使用“for each”。
#4
2
There's no for each in
in the version of ECMAScript supported by Node.js, only supported by firefox currently.
在节点支持的ECMAScript版本中,每个版本都没有。js,目前仅支持firefox。
The important thing to note is that JavaScript versions are only relevant to Gecko (Firefox's engine) and Rhino (which is always a few versions behind). Node uses V8 which follows ECMAScript specifications
需要注意的重要一点是,JavaScript版本只与Gecko (Firefox的引擎)和Rhino(它后面总是有几个版本)相关。Node使用V8,它遵循ECMAScript规范
#1
102
Unfortunately node does not support for each ... in
, even though it is specified in JavaScript 1.6. Chrome uses the same JavaScript engine and is reported as having a similar shortcoming.
不幸的是,node并不支持每一个…即使在JavaScript 1.6中指定了它。Chrome使用相同的JavaScript引擎,并被报告有类似的缺点。
You'll have to settle for array.forEach(function(item) { /* etc etc */ })
.
你将不得不满足于array.forEach(函数(项){/*等*/})。
EDIT: From Google's official V8 website:
编辑:来自谷歌官方V8网站:
V8 implements ECMAScript as specified in ECMA-262.
V8实现了ECMAScript,如ECMA-262中指定的那样。
On the same MDN website where it says that for each ...in
is in JavaScript 1.6, it says that it is not in any ECMA version - hence, presumably, its absence from Node.
在同一个MDN网站上,它说每个…在JavaScript 1.6中,它说它不在任何ECMA版本中——因此,可以推测,它不在Node中。
#2
61
for (var i in conf) {
val = conf[i];
console.log(val.path);
}
#3
6
https://github.com/cscott/jsshaper implements a translator from JavaScript 1.8 to ECMAScript 5.1, which would allow you to use 'for each' in code running on webkit or node.
https://github.com/cscott/jsshaper实现了一个从JavaScript 1.8到ECMAScript 5.1的转换器,它允许你在webkit或节点上运行的代码中使用“for each”。
#4
2
There's no for each in
in the version of ECMAScript supported by Node.js, only supported by firefox currently.
在节点支持的ECMAScript版本中,每个版本都没有。js,目前仅支持firefox。
The important thing to note is that JavaScript versions are only relevant to Gecko (Firefox's engine) and Rhino (which is always a few versions behind). Node uses V8 which follows ECMAScript specifications
需要注意的重要一点是,JavaScript版本只与Gecko (Firefox的引擎)和Rhino(它后面总是有几个版本)相关。Node使用V8,它遵循ECMAScript规范