意外的标记 :

时间:2018-04-27 18:18:53

标签: javascript

 <script>
    function createPerson(firstName, lastName)
    {
        return
        {
            firstName: firstName,
            lastName: lastName, //This line!!!
            getFullName: function() {
                return this.firstName + " " + this.lastName;
            },
            greet: function(person) 
            {
             alert("Hello, " + person.getFullName() + "I'm " + this.getFullName());
            }

        };
    }
    var johnDoe = createPerson("John" , "Doe");
    var janeDoe = createPerson("Jane" , "Doe");

    johnDoe.greet(janeDoe);
</script>

为什么这一行会抛出错误? “意外的标记 :”。这是一本书的例子,我做了完全相同的事情,但这个错误出现了。不知道出了什么问题。

4 个答案:

答案 0 :(得分:5)

您无法在return声明前打破一行。

&#13;
&#13;
function createPerson(firstName, lastName)
{
    return {
        firstName: firstName,
        lastName: lastName, //This line!!!
        getFullName: function() {
            return this.firstName + " " + this.lastName;
        },
        greet: function(person) 
        {
         console.log("Hello, " + person.getFullName() + "I'm " + this.getFullName());
        }

    };
}
var johnDoe = createPerson("John" , "Doe");
var janeDoe = createPerson("Jane" , "Doe");

johnDoe.greet(janeDoe);
&#13;
&#13;
&#13;

答案 1 :(得分:3)

删除return后的换行符。由于JavaScript的自动分号插入,新行被视为;

<强>演示

function foo() {
  return
  'foo';
}
console.log(foo()) // undefined

function bar() {
  return 'bar';
}
console.log(bar()) // bar

您的代码段已修复

function createPerson(firstName, lastName)
{
    return {
        firstName: firstName,
        lastName: lastName,
        getFullName: function() {
            return this.firstName + " " + this.lastName;
        },
        greet: function(person) 
        {
         console.log("Hello, " + person.getFullName() + "I'm " + this.getFullName());
        }

    };
}
var johnDoe = createPerson("John" , "Doe");
var janeDoe = createPerson("Jane" , "Doe");

johnDoe.greet(janeDoe);

详细说明实际错误:

  

{..}被解释为一个块。 firstName:被解释为   标签。 firstName, lastName: lastName被解释为逗号   期望两个表达式的运算符,lastName: lastName不是a   有效表达式,因为此处:无效。因此错误   “意外的令牌:”。 - Felix Kling

答案 2 :(得分:3)

你有

return
{

Javascript具有自动分号插入功能;对于interpter,你的代码看起来像

return;
{

因为{位于 next 行,并且因为它本身就是合法的。

{放在与return相同的行上,而不是:

return {
  // ...

答案 3 :(得分:2)

它失败了因为它等同于:

function createPerson(firstName, lastName)
{
    return;
    {
        firstName: firstName,
        lastName: lastName, //This line!!!
        getFullName: function() {
            return this.firstName + " " + this.lastName;
        },
        greet: function(person) 
        {
         alert("Hello, " + person.getFullName() + "I'm " + this.getFullName());
        }

    };
}
var johnDoe = createPerson("John" , "Doe");
var janeDoe = createPerson("Jane" , "Doe");

johnDoe.greet(janeDoe);

由于自动分叉插入。这就是为什么更常见的风格是在同一条线上打开蕨菜。

function createPerson(firstName, lastName) {
    return {
        firstName: firstName,
        lastName: lastName, //This line!!!
        getFullName: function() {
            return this.firstName + " " + this.lastName;
        },
        greet: function(person) {
         alert("Hello, " + person.getFullName() + "I'm " + this.getFullName());
        }

    };
}
var johnDoe = createPerson("John" , "Doe");
var janeDoe = createPerson("Jane" , "Doe");

johnDoe.greet(janeDoe);