<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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With