I'm using node.js
which I installed from its website according to the instructions available here . I tried to execute this example from the "JavaScript - The Good Parts" textbook:
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);
However, when I call node test.js
(the name of the file this is in), I get the following error:
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
This is the exact example given, which is why I'm confused as to why this isn't working. Am I missing something?
Object literals key-value pairs are separated using commas, not semicolons. Instead of this:
var myObject = {
value: 0;
increment: function (inc) {
this.value += (typeof inc) === 'number' ? inc : 1;
}
};
Use this:
var myObject = {
value: 0,
increment: function (inc) {
this.value += (typeof inc) === 'number' ? inc : 1;
}
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.