为什么在属性名称中引号引发错误?

当我执行一个包含{"test":1} node.js文件时,会引发一个SyntaxError:

 (function (exports, require, module, __filename, __dirname) { {"test":1} ^ SyntaxError: Unexpected token : at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:902:3 

{test:1} (不含引号)或var t = {"test":1}正常工作。

我执行包含运行的代码的文件: node test.js

为什么?

{}形成一个块 。

test:是一个标签 ,这是有效的(但毫无意义,因为没有循环)。

"test":是一个string,后面跟着一个冒号,这是无稽之谈。

var foo = {}{}置于不同的上下文中,因此它们形成对象字面值而不是块。 在对象文字中,属性名称可以是标识符或string。


你的test.js内容看起来像JSON,而不是JavaScript。 执行它是没有意义的,因为它什么都不