Reputation: 39
<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
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
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
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
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