0

I'm modifying a Javascript library which was written by a third party. The code does work for the most part. There are no syntax errors. It will be nearly impossible to ask the author about the code.

I came across this construct, and cannot understand what the programmer intended.

pages[uuid, elIndex] = 12;

Inspecting with Chrome devtools, I see that page is an object. uuid is a string and elIndex is a number.

I can't understand what's intended here with the comma between multiple object properties. Is this some very obscure Javascript syntax? What is meant by accessing "multiple properties" like this?

Charlie Dalsass
  • 1,986
  • 18
  • 23
  • Maybe the programmer thought that this was multidimensional array access. [It isn't.](https://stackoverflow.com/q/20645191/1048572) But without further code, we can only guess either. – Bergi Jul 14 '17 at 16:36
  • This seems like it would be mistake. What this does is just set both of the properties ("uuid" and "elIndex") to 12. I imagine that setting the uuid to 12 was not intended. – Alex Bieg Jul 14 '17 at 16:36
  • @AlexBieg no, it only sets `pages[elINdex] = 12`. The comma operator has lower precedence than array acces – c2huc2hu Jul 14 '17 at 16:37
  • @user3080953 Oops you are correct. I messed up my little test in the console. Sorry about that. – Alex Bieg Jul 14 '17 at 16:40
  • @user3080953 is correct. When I inspect in Chrome, it's as if the first parameter is ignored. Maybe I should just chalk it up to a sloppy programmer - though the library is pretty well written IMO - not a beginner. – Charlie Dalsass Jul 14 '17 at 16:40
  • Considering `pages` is an object and `uuid` and `elIndex` are the Keys of associated array. Effectively it is equivalent to `pages[elIndex]=12` You can try this: `var pages = {uuid: "text: uuid", elIndex: "text: elIndex"}; uuid = "uuid"; elIndex="elIndex"; pages[uuid, elIndex] = 12; console.log(pages);` – Om Sao Jul 14 '17 at 16:42
  • From the names of the variables, pages being a collection of pages, uuid being an id for a page, and elIndex being the index of an element on a certain page, and the attempt to set something to a numeric value (12), I would guess `pages[uuid][elIndex] = 12` was meant. – James Jul 14 '17 at 16:42
  • 1
    @OmSao your understanding is incorrect, user3080953 explains why. – Jared Smith Jul 14 '17 at 16:45
  • Thanks everyone. Consensus seems to be that the programmer was just confused - even though the syntax was used throughout the library. But what's important (to me) is that this is not some super quirky aspect of Javascript (I am unaware of). – Charlie Dalsass Jul 14 '17 at 16:45
  • @CharlieDalsass not necessarily, as there are a million different ways for legal syntax snippets to interact oddly and the grammar is complicated enough as it is. – Jared Smith Jul 14 '17 at 16:48
  • Agreed this should not raise a Javascript error, now that I understand how "comma operator" works. Thanks! – Charlie Dalsass Jul 14 '17 at 16:51

1 Answers1

2

This:

pages[uuid, elIndex] = 12;

is exactly equivalent to:

pages[elIndex] = 12;

This is the seldom-used comma operator.

Unlike a method call (which takes several arguments separated by commas), the array indexer takes a single expression, so uuid, elIndex is parsed as a single expression that uses the comma operator. The comma operator evaluates both sides, then throws away the value of the left side and returns the value of the right side. It's occasionally used in for-loop initializers, and it's used heavily in minified code, but otherwise it really doesn't have a ton of practical applications if you're trying to write readable code.

As Bergi noted in the comments above, the author probably thought this would be a multidimensional array access, but it isn't. It's just pages[elIndex].

Joe White
  • 94,807
  • 60
  • 220
  • 330
  • 1
    Thanks everyone. Great help. This actually looks like a similar one to https://stackoverflow.com/questions/20645191/undestanding-double-bracket-operation-in-javascript Perhaps the programmer was so familiar with the comma operator that he threw that in sort of like a "to do", assuming it wouldn't confuse the heck out of other programmers. – Charlie Dalsass Jul 14 '17 at 16:50