使用node.js和教科书示例的意外令牌错误

我正在使用node.js ,根据这里提供的说明从我们的网站安装。 我试图从“JavaScript – The Good Parts”教程中执行这个例子:

 var myObject = { value: 0; increment: function (inc) { this.value += (typeof inc) === 'number' ? inc : 1; } }; myObject.increment( ); document.writeln(myObject.value); myObject.increment(2); document.writeln(myObject.value); 

但是,当我调用node test.js (这是文件的名称),我得到以下错误:

  value: 0; ^ 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 

这是确切的例子,这就是为什么我不明白为什么这不起作用。 我错过了什么吗?

对象文字键值对使用逗号分隔,而不是分号。 而不是这个:

 var myObject = { value: 0; increment: function (inc) { this.value += (typeof inc) === 'number' ? inc : 1; } }; 

用这个:

 var myObject = { value: 0, increment: function (inc) { this.value += (typeof inc) === 'number' ? inc : 1; } };