javascript - nodeJS syntaxError: unexpected token this - Stack Overflow

Background: User inputs details about a flight in app.js, which then prints it out in the console. Only

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.

Problem: When I run the mand "node app.js", I get the following error:

/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._pile (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._pile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)

This is the index.js code:

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:

     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.

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.

Problem: When I run the mand "node app.js", I get the following error:

/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._pile (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._pile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)

This is the index.js code:

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:

     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.

Share Improve this question edited Oct 14, 2015 at 21:17 Frosty619 asked Oct 14, 2015 at 21:14 Frosty619Frosty619 1,4894 gold badges24 silver badges36 bronze badges 1
  • As an error says you have ` var this.data = {`. Using var here is an error, remove it – Vsevolod Goloviznin Commented Oct 14, 2015 at 21:16
Add a ment  | 

1 Answer 1

Reset to default 5

var is used to create a new variable, not to create a property on an object.

Remove var from the line that is erroring.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744125473a4559595.html

相关推荐

  • javascript - nodeJS syntaxError: unexpected token this - Stack Overflow

    Background: User inputs details about a flight in app.js, which then prints it out in the console. Only

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信