简体   繁体   English

在Node.js中返回对象文字时出错

[英]Error returning an object literal in Node.js

I have the following code in my application as i am following lynda.com tutorial to learn Node.js I get some error on line where it says origin: o saying "unexpected token" 我在lynda.com教程中学习Node.js时,在我的应用程序中有以下代码:在出现源代码的地方出现一些错误:o说“意外令牌”

var number, origin, destination;

exports.setNumber = function(num){
    number = num;
}

exports.setOrigin = function(o){
    origin = o;
}

exports.setDestination = function(d){
    destination = d;
}

exports.getInfo = function(){
    return 
    {
        number: number,
        origin: origin,
        destination: destination
    };
};

I have no idea what is the error, i am following the tutorial line by line on lynda.com 我不知道这是什么错误,我在lynda.com上逐行遵循教程

return 
{ ... }

is equivalent to 相当于

return;
{ ... }

because of JavaScript's automatic semicolon insertion . 由于JavaScript 自动插入了分号 If you want to spread the return value over multiple lines, you have to start the object literal on the same line: 如果要将返回值分布在多行中,则必须在同一行上启动对象文字:

return {
  // ...
};

You got the error because 您收到错误消息是因为

{
    number: number,
    origin: origin,
    destination: destination
};

is interpreted as a block , number: as a label and t he , as a sequence expression , which is basically equivalent to 被解释为一个 ,一个number:一个标签 ,一个他,作为一个序列表达式 ,基本上等价于

(number, origin: origin, destination: destination)

origin: is simply invalid at this position. origin:在此位置上完全无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM