javascript - Incrementing the result of Map.get() -
i following error when trying increment value obtained map in console in chrome (50.0.2661.86):
uncaught referenceerror: invalid left-hand side expression in postfix operation(…)
and similar in node (4.4.3):
referenceerror: invalid left-hand side expression in postfix operation @ repl:1:3 @ replserver.defaulteval (repl.js:262:27) @ bound (domain.js:287:14) @ replserver.runbound [as eval] (domain.js:300:12) @ replserver.<anonymous> (repl.js:431:12) @ emitone (events.js:82:20) @ replserver.emit (events.js:169:7) @ replserver.interface._online (readline.js:211:10) @ replserver.interface._line (readline.js:550:8) @ replserver.interface._ttywrite (readline.js:827:14)
the offending code is:
var m = new map() m.set(1, 0) m.get(1) var n = m.get(1)++ // uncaught referenceerror: invalid left-hand side expression in postfix operation(…)
the following fail:
var n = ++m.get(1) var n = ++(m.get(1))
a bug in v8 maybe? or misunderstanding in what's happening syntax-wise ++
operator?
i'm not expert in js evaluation scheme, here's try @ explaining that.
in js, (1)++
invalid; can't change a value. can change value attribution name:
var = 1; a++;
in case value of 1
doesn't change; changes a
points different value.
similarly, can't increment value returned .get()
; need convert named expression before can change that.
in c++ terms ++
needs lvalue, function return rvalue.
Comments
Post a Comment