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 9 of 9

Thread: Syntax Question

  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Syntax Question

    if (n==1){food1=temp}
    if (n==2){food2=temp}

    I need to expand the above by many times, so I am wondering if I can make it more efficient.

    I tried the following but got a syntax error:

    "food"+n=temp;

    Thanks, Peter

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Why not
    Code:
    var
        food=[];
    food[n]=temp;
    ?
    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

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Can you say in English what var food=[]; is doing?

    P.S.

    It works perfectly! Thanks!!
    Last edited by peterinwa; 06-05-2003 at 04:40 PM.

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    It does the same as var food=new Array; would. In other words, it makes food an array containing 0 elements.
    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
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks. I don't like to use code I don't understand.

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kansas, USA
    Posts
    487
    Thanks
    0
    Thanked 54 Times in 53 Posts
    var food=[];
    is assigning the new variable food an empty array. The side effect is that the variable food is cast as an array object.

    It is a shorthand for this declaration
    var food = new Array();

    Edit: bah! damn my slow brain

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The side effect is that the variable food is cast as an array object.
    Are you suggesting that this is a bad thing? If so why?

  • #8
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Actually, it's not a side effect. It's a type declaration combined with a definition. It would only be a side effect if we were doing it on a previously existing object, which is not the case. But that's technicalities.

    No, it's not a bad thing. It is in fact the very thing we're using to achieve what we want.
    Last edited by liorean; 06-07-2003 at 01:52 AM.
    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

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by liorean
    Actually, it's not a side effect. It's a type declaration combined with a definition. It would only be a side effect if we were doing it on a previously existing object, which is not the case. But that's technicalities.

    No, it's not a bad thing. It is in fact the very thing we're using to achieve what we want.
    OK. Thanks. Just making sure I'm not missing something here.


  •  

    Posting Permissions

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