Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Is the switch expression evaluated only once?

    If you compare:

    switch(a * b){...}

    to:

    var expression = a * b;
    switch(expression){...}


    does storing 'expression' serve any purpose?
    *this message will self destruct in n-seconds*

  • #2
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    function foo()
    {
    var a = 1;
    var b = 2;
    switch(a * b)
    {
    case 2: alert("evaluated once");a = 2;
    case 4: alert("evaluated again");
    }
    }


    I guess so, if that test is telling me what it seems...

    Edit: oops: actually it does the same thing without a = 2;... looks like i need a better test!
    Last edited by codegoboom; 08-27-2004 at 09:14 AM.
    *this message will self destruct in n-seconds*

  • #3
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So it doesn't matter, because you have to use break; or else the case is not even checked, er something... (that's my scientific conclusion )... might as well do without the variable.

    [slapMe]
    shoot, the manual did mention that (looking back...) & what a pointless question (yet again); but who knows... maybe one of these goose chases will lead to something worthwhile...
    awe fooey!
    [/slapMe]
    Last edited by codegoboom; 08-27-2004 at 10:21 AM.
    *this message will self destruct in n-seconds*

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    JavaScript is send-by-value - what this mean is that there is no difference between
    Code:
    var
        c=a*b;
    switch(c)
    and
    Code:
    switch(a*b)
    from the switch statement's point of view. All expressions are evaluated before they are sent to the containing expression or statement.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    As opposed to send-by-ref. (correct?), whatever that actually means; and referenced values are ideal in loops...
    *this message will self destruct in n-seconds*

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Actually it just struck me the reason is not at all that it's send-by-value. The reason is that it's eagerly evaluated instead of lazily evaluated.

    In a way, you could say that in JavaScript, everything is evaluated from deepest nested to shallowest nested. This means that expressions are evaluated before they are sent to their surrounding expressions or statements, so the shallower statement or expression is never even aware that the value it's been sent was generated by evaluating something.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, I noticed that... which makes it easy to string together compound references without needing a bunch of variables for each.
    I really have no business sticking my nose behind the curtain, yet, anyway--but some ideas just never become clear until you can have a gaze at the whole picture...
    *this message will self destruct in n-seconds*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •