Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected token :

Tags:

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>

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.

like image 254
i11_1997 Avatar asked May 13 '26 06:05

i11_1997


1 Answers

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);
like image 165
DontVoteMeDown Avatar answered May 15 '26 20:05

DontVoteMeDown