Function assigned to var wants extra semicolon

Discussion in 'Javascript' started by Lawrence San, Oct 18, 2006.

  1. Lawrence San

    Lawrence San Guest

    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?

    --
    Lawrence San
    Cartoon Stories for Thoughtful People:
    <http://www.sanstudio.com>
     
    Lawrence San, Oct 18, 2006
    #1
    1. Advertising

  2. Lawrence San

    Lee Guest

    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.


    --
     
    Lee, Oct 18, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alvin Bruney

    Threads.. Session var lost, App var ok

    Alvin Bruney, Dec 2, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    365
    rooster575
    Dec 2, 2003
  2. JulioNobre
    Replies:
    0
    Views:
    6,927
    JulioNobre
    Sep 26, 2006
  3. mathieu
    Replies:
    3
    Views:
    598
    Bo Persson
    Sep 4, 2009
  4. Peter Michaux

    Cookies: semicolon vs. semicolon-space

    Peter Michaux, Dec 10, 2006, in forum: Javascript
    Replies:
    3
    Views:
    221
    Michael Winter
    Dec 11, 2006
  5. prati
    Replies:
    0
    Views:
    450
    prati
    Oct 27, 2012
Loading...

Share This Page