i11_1997
i11_1997

Reputation: 39

Unexpected token :

 <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>

Why does this line throw an error ? "Unexpected token :". This is an example from a book, I did exactly the same thing but this error is showing up. Don't know what's wrong.

Upvotes: 3

Views: 105

Answers (4)

Kristianmitk
Kristianmitk

Reputation: 4778

Remove the newline after the return. As a newline is treated as an ; due to JavaScript's automatic semicolon insertion

Demo

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

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

Your snippet fixed

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);


To elaborate on the actual error:

{..} is interpreted as a block. firstName: is interpreted as a label. firstName, lastName: lastName is interpreted as the comma operator which expects two expressions, lastName: lastName is not a valid expression because : is not valid here. Hence the error "Unexpected token :". – Felix Kling

Upvotes: 3

CertainPerformance
CertainPerformance

Reputation: 371193

You have

return
{

Javascript has Automatic Semicolon Insertion; to the interpter, your code looks like

return;
{

because the { is on the next line, and because it's legal to exist on its own.

Put the { on the same line as the return instead:

return {
  // ...

Upvotes: 3

generalhenry
generalhenry

Reputation: 17319

It fails because it's equivalent to:

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);

due to automatic semi-colon insertion. Which is why the more common style is opening bracken on the same line.

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);

Upvotes: 2

DontVoteMeDown
DontVoteMeDown

Reputation: 21475

You can't break a line in front of a return statement.

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);

Upvotes: 5

Related Questions