[英]Unexpected token error using node.js with a textbook example
我正在使用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
(文件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;
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.