Function assigned to var wants extra semicolon

L

Lawrence San

According to a JavaScript debugger (Firebug), and to a JS lint, this is
fine:

function recalc(){deriv = 6;} [more code here]

But, if I've assigned the function to a variable like this:

var bells = function recalc(){deriv = 6;} [more code here]

.... then both the debugger and the lint report an error, saying there's
a missing semicolon after the close-curly-brace. They say I should do
this:

var bells = function recalc(){deriv = 6;}; [more code here]

They must be right, because the code works the second way but not the
first way. But why? Isn't a close-curly-brace supposed to be sufficient
to indicate the end of a statement? Usually, if you put a semicolon
after a close-curly-brace like this:

function recalc(){deriv = 6;}; [more code here]

....the lint complains "Warning: empty statement or extra semicolon".
But if the beginning of the statement includes a variable assignment,
it wants the extra semicolon at the end. Why?
 
L

Lee

Lawrence San said:
According to a JavaScript debugger (Firebug), and to a JS lint, this is
fine:

function recalc(){deriv = 6;} [more code here]

But, if I've assigned the function to a variable like this:

var bells = function recalc(){deriv = 6;} [more code here]

... then both the debugger and the lint report an error, saying there's
a missing semicolon after the close-curly-brace. They say I should do
this:

var bells = function recalc(){deriv = 6;}; [more code here]

They must be right, because the code works the second way but not the
first way. But why? Isn't a close-curly-brace supposed to be sufficient
to indicate the end of a statement? Usually, if you put a semicolon
after a close-curly-brace like this:

function recalc(){deriv = 6;}; [more code here]

...the lint complains "Warning: empty statement or extra semicolon".
But if the beginning of the statement includes a variable assignment,
it wants the extra semicolon at the end. Why?

One is a function definition, which logically ends with a closing bracket.
The other is an assignment statement.
The fact that what you have on the right-hand side of this
assignment happens to be a function definition doesn't change that.


--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top