Results 1 to 8 of 8

Thread: Variables with a Name that's a String?

  1. #1
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Variables with a Name that's a String?

    I asked my friend about this once... he said you do:

    var this["astringforthename"] = "blah";
    alert(var this["astringforthename"]);

    I tried that in Firefox, and it didn't work. Can someone either tell me why it didn't work and/or how you make a variables name be a string?

  2. #2
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    this is a reserved keyword and you also need to initialize the array before you can use it. Instead do:

    Code:
    var that = [];
    that["key"] = "value";
    and you don't need the var inside the alert:

    Code:
    alert(that["key"]);
    Combined:

    Code:
    var that = [];
    that["key"] = "value";
    alert(that["key"]);

  3. #3
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default

    Opse, I didnt mean to have the var in the alert part, sorry.

  4. #4
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default

    So it's just an array, but instead of having a numbers, you have strings? So you have to define a variable?

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Don't do this with arrays. All objects in ECMAScript can be modified in this way. Using Array is a bad choice because ECMAScript arrays are meant to be unique, and it's entirely reasonable for a script to extend Array, breaking the only method of enumeration available for such hashes, the for..in loop. Instead, either use a plain Object:
    Code:
    var myhash = {};
    myhash['fish'] = 5;
    
    (print || alert)(myhash['fish']); // 5
    (print || alert)(myhash.fish); // 5
    This has the added advantage that you can use the standard object literal:
    Code:
    var myhash = {
      'fish' : 5
    };
    
    (print || alert)(myhash['fish']); // 5
    (print || alert)(myhash.fish); // 5
    You can also create your own Hash constructor, or use one of the ones available on the Internet (have a look for Rick Measham's version, in particular: I can't seem to find it, but I remember it being a fairly well-implemented one). This option allows you to create new methods for your hashes without the fear of breaking existing code (always try to avoid extending Object).
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Code:
    var myhash = {
      'fish' : 5
    };
    Is there a reason to put quotes around fish? It seems to work fine without them when referencing them with dot notation. Or is it so you can reference things like '* gsd s\'s' with the bracket notation?

    Oh, and what's the difference between an object hierarchy and a hash hierarchy?

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I can't remember the exact grammar as to why not using quotes works here, perhaps it's just a special case. Either way, using quotes is (in my opinion) neater, and allows one to have property names containing special characters such as spaces, which is obviously desirable in a hash.
    Oh, and what's the difference between an object hierarchy and a hash hierarchy?
    In ECMAScript all objects are also hashes, so nothing.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Trinithis View Post
    Code:
    var myhash = {
      'fish' : 5
    };
    Is there a reason to put quotes around fish?
    Not when the property name conforms to the Identifier grammar production, or it is a number. Otherwise, quotes are necessary.
    Mike

Bookmarks

Posting Permissions

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