-1

Recently I posted about Dynamic Names in Javascript. I went ahead and tried to make a multi string variable name (combining a string and a variable to make a new variable name), and it does not seem to work. I am very confused because I am doing what many posts on SO say to do (so I think anyhow).

Anyhow here is the dynamic variable I am using:

var dynamic["replyupvote"+replyid] = false;

and then when I am calling it I use:

dynamic["replyupvote"+replyid]

So my question is where am I going wrong? If you would like to see my full code:

function replyupvote(replyid, upvotes, downvotes, votesclass, votesnumber) {
    var dynamic["replyupvote"+replyid] = false;
    return function() {
        if (dynamic["replyupvote"+replyid]) {
            dynamic["replyupvote"+replyid] = true;
        }
        else {
            $.ajax({
                url: "http://localhost/postin'/categories/votes.php",
                type: "POST",
                data: { 'itemid': replyid,
                        'userid': <?php echo $_SESSION["logged_in"]; ?>,
                        'action': "upvotes",
                        'type': "reply" },
                success: function() {
                    $("." + votesclass).css("color", "orange");
                    $("." + votesnumber).text(parseInt(upvotes - downvotes) + 1);
                }
            });
            dynamic["replyupvote"+replyid] = true;
        }
    }
}

This code worked before I through in the Multi String Variable Names. So what am I doing wrong? Thank You! :)


EDIT

I thought I should throw this in. Javascript throws the error that the function is not defined because of the incorrect syntax.

Michael Jones
  • 2,222
  • 18
  • 32
  • There's no such thing as dynamic variable names in JS. You're conflating two different concepts. There are object properties that can be stored and retrieved dynamically. –  Jun 26 '15 at 02:08
  • Oh, well what I really mean is just combining a string and a variable for a new variable name. – Michael Jones Jun 26 '15 at 02:10
  • You're storing your data in `dynamic` object, right? How did you define it? Using `dynamic[varname]` should work. – TaoPR Jun 26 '15 at 02:11
  • 1
    What I mean is that you can't do that. You can do it with object properties, but not variables. –  Jun 26 '15 at 02:12
  • Oh, I was looking at these posts: http://stackoverflow.com/questions/31063324/double-string-variable-name-javascript?noredirect=1#comment50146907_31063324 and http://stackoverflow.com/questions/22727711/javascript-dynamic-variable-names?lq=1 – Michael Jones Jun 26 '15 at 02:13
  • Why don't you just use an object with a custom property name? What you're doing seems way, way more complicated than it should be. – jfriend00 Jun 26 '15 at 02:14
  • You're not using the syntax defined in those answers. The first one (your earlier question) is technically valid, but doesn't make sense. Follow the second one. –  Jun 26 '15 at 02:15
  • When you describe the actual problem you're trying to solve, we can help with a much, much better solution. – jfriend00 Jun 26 '15 at 02:15
  • I don't even know why you'd need to use `replyid` as part of the variable name. It's a local variable and the only one in that function. –  Jun 26 '15 at 02:16
  • I would like to say Thank You. Honestly I just looked at what I was doing, and realized it made no sense. Thank you for making me see that! :) – Michael Jones Jun 26 '15 at 02:18

2 Answers2

1

Regardless if what you are doing here makes sense or not, to dynamically create properties on an object, you will need to make sure JS knows it's an object, not an array. So before you try to create a dynamic object property, explicitly declare dynamic as an object:

var dynamic = {};
dynamic["replyupvote"+replyid] = false;

That should get rid of the syntax error at least.

Tony
  • 388
  • 3
  • 11
1

You would have to set dynamic as an object first

var dynamic = {};
dynamic["replyupvote"+replyid] = false;

variableName[keyName] = value; is the syntax of an object.

You have to tell js that your variable is an object before you can use this notation.

Corey Young
  • 520
  • 4
  • 10
  • Unfortunately, @Tony posted an identical answer a minute before you did, so you have the burden of needing to make your answer more original – Drakes Jun 26 '15 at 02:33
  • story of my stackoverflow life, always 30s to 2mins late with the post. I guess I will need to work on my answer speed lol – Corey Young Jun 26 '15 at 02:34