javascript - Operator assigning with a pre-increment -


for looping variables (usually array indexes) lot of pre-incrementing , modulus assigning in loop. in other languages like

++_index %= length; 

(broken version on multiple lines)

_index++; _index = _index % length; 

this of course increases _index 1 , assigns modulus itself.

but have problems doing in javascript horrible referenceerror: invalid left-hand side in assignment.

i suspect doing trying assign modulus of variable called "++_index" of course, there none invalid.

i tried using parentheses have read has _index correctly still seemed bust.

(++_index) %= length; 

i have resorted using broken down version of

++_index; _index %= length; 

but cannot find documentation on issue @ hand. google-fu bringing people using = within if statements.

could far superior google-fu direct me documentation can explain in more depth. said, suspect trying assignment ++_index, rather evaluate it, increment , pass reference variable next stage of assigning modulus. (or someone) confirm or deny beneficial.

the javascript prefix increment operator doesn't return reference, returns value. it's invalid assign number, that's why throws referenceerror.

you either have this:

_index = ++_index % length; 

or this:

++_index; _index %= length; 

specification:

  1. let expr result of evaluating unaryexpression.
  2. let oldvalue tonumber(getvalue(expr)).
  3. returnifabrupt(oldvalue).
  4. let newvalue result of adding value 1 oldvalue, using same rules + operator (see 12.7.5).
  5. let status putvalue(expr, newvalue).
  6. returnifabrupt(status).
  7. return newvalue.

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -