[英]nodeJS syntaxError: unexpected token this
背景 :用戶在app.js
輸入有關航班的詳細信息,然后在控制台中將其打印出來。 index.js
僅使用一個模塊,其中包含一個對象原型。
問題 :運行命令“ 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)
這是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;
};
這是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());
我不知道我要去哪里錯了。 據我了解,我創建對象原型的方式是正確的。
var
用於創建新變量,而不是在對象上創建屬性。
從出錯的行中刪除var
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.