javascript nodeJS 语法错误:意外令牌此
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33135695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
nodeJS syntaxError: unexpected token this
提问by Frosty619
Background: User inputs details about a flight in app.js
, which then prints it out in the console. Only one module is used from index.js
, which contains an object prototype.
背景:用户在 中输入有关航班的详细信息app.js
,然后在控制台中将其打印出来。只使用了一个模块 from index.js
,它包含一个对象原型。
Problem: When I run the command "node app.js", I get the following error:
问题:当我运行命令“node app.js”时,出现以下错误:
/Users/
UserName/Desktop/NodeTrainingWork/04/objectcreat/flight/index.js:3
var this.data = {
^^^^
SyntaxError: Unexpected token this
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:413:25)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/Users/UserName/Desktop/NodeTrainingWork/04/objectcreat/app.js:1:76)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
This is the index.js
code:
这是index.js
代码:
var Flight = function() {
var this.data = {
number: null,
origin: null,
destination: null,
arrivalTime: null
};
this.fill = function(info) {
for (var prop in this.data) {
if (this.data[prop] != 'undefined') {
this.data[prop] = info[prop];
}
}
};
this.triggerArrival: function() {
this.data.arrivalTime = Date.now();
}
this.getInformation: function() {
return this.data;
}
};
module.exports = function(info) {
var instance = new Flight();
instance.fill(info);
return instance;
};
And this is the code in the app.js
file:
这是app.js
文件中的代码:
var flight = require('./flight');
var pdxlax = {
number: 847,
origin: 'PDX',
destination: 'LAX'
};
var pl = flight(pdxlax);
pl.triggerArrival();
console.log(pl.getInformation());
var pk340 = {
number: 340,
origin: 'ISL',
destination: 'DXB'
};
var pk = flight(pk340);
pk.triggerArrival();
console.log(pk.getInformation());
I dont know where I am going wrong. From what I have learned, my way of creating a an object prototype is correct.
我不知道我哪里出错了。据我所知,我创建对象原型的方式是正确的。
回答by Quentin
var
is used to create a new variable, not to create a property on an object.
var
用于创建新变量,而不是在对象上创建属性。
Remove var
from the line that is erroring.
var
从出错的行中删除。