1

Currently I can do:

function addStat() {
player.str = player.str + 1;
}

But I want to be able to use things other than just "str" with my player object. So I decided with doing something like this:

function addStat(stat) {
player.stat = player.stat + 1;
}

But that doesn't seem to work, iv'e tried looking up the syntax for using parameters but could not find anything similar to the way I need.

I learned about "this" but I can't get it to work with my function.

I thought this:

function addStat(thing, stat) {
thing.stat = thing.stat + 1;
statReset();
}

would work but I can see why it won't. I made sure the rest of my javascript and html work and when I add those functions nothing breaks, it just doesn't work.

Roman P
  • 35
  • 3
  • 1
    possible duplicate of [Dynamically access object property using variable](http://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – Jamiec Feb 12 '15 at 16:06

3 Answers3

4

When assigning properties with a variable, you need to use bracket notation, as opposed to dot notation. This, then, looks like:

function addStat(stat) {
    (stat in player) ? ++player[stat] : player[stat] = 1;
}

Due to comments (that I disagree with), I figured I should mention that since you are attempting to modify a property that may not exist, you should also add a safety check to see if you can modify it.

Otherwise you will be modifying undefined, and that will cause undesired output..

ndugger
  • 7,373
  • 5
  • 31
  • 42
  • @Andy -- while your comment is valid, it has nothing to do with the current question at hand. – ndugger Feb 12 '15 at 16:07
  • 2
    @Jamiec it works for _setting_ new properties, it doesn't work for _modifying_ properties that don't yet exist. – Alnitak Feb 12 '15 at 16:08
  • @Alnitak - How can you *modify* something that doesnt exist? – Jamiec Feb 12 '15 at 16:09
  • Do I need to make the object with that notation also? Currently I have it like: obj { obj.value = x } – Roman P Feb 12 '15 at 16:09
  • @Jamiec _exactly!_. A simple work around is `player[stat] = (player[stat] + 1) || 1` – Alnitak Feb 12 '15 at 16:10
  • Guys, as I said, while the comment is valid, it has nothing to do with the current question. The concern was about the syntax. – ndugger Feb 12 '15 at 16:12
  • @RomanP no, you can't use variables as keys in "object literal" syntax – Alnitak Feb 12 '15 at 16:12
  • 1
    @NickDugger, I disagree. The OP is moving from using specific keys to possibly passing in a parameter for a key that doesn't yet exist. I think that's worth mentioning. – Andy Feb 12 '15 at 16:14
  • @Andy while I entirely disagree, I've modified the post to reflect this discussion. – ndugger Feb 12 '15 at 16:16
  • 1
    @NickDugger, if you disagree with it you shouldn't add it to your answer. That's just weird, man. – Andy Feb 12 '15 at 16:18
0

You can access properties with []:

function addStat(prop) {
    player[prop] = player[prop] + 1;
}

so calling addStat("stat") will actually set player.stat.

LS_ᴅᴇᴠ
  • 10,823
  • 1
  • 23
  • 46
0

In javascript, the syntax

object.key

is equivalent to

object["key"]

So your thing.stat is equivalent to thing["stat"], i.e. the key is the literal string "stat" when what you really want is to use the value referenced by the parameter stat as the key:

thing[stat] = thing[stat] + 1;
CupawnTae
  • 14,192
  • 3
  • 29
  • 60