javascript - Left hand side expression increments even though exception is thrown -
given following code snippet:
const arr = []; let pos = 0; const fn = () => { throw 'foo'; }; try { arr[pos++] = fn(); } catch (e) { console.log(pos); }
i surprised see value of pos
1. intuitively, have expected rhs evaluated first, when exception thrown pos
remain unmodified.
could point relevant portions of spec covers this? or otherwise explain the behaviour?
ecmascript 2015 defines behavior:
assignmentexpression[in, yield] : lefthandsideexpression[?yield] = assignmentexpression[?in, ?yield]
if lefthandsideexpression neither objectliteral nor arrayliteral, then
a. let lref result of evaluating lefthandsideexpression.
b. returnifabrupt(lref).
c. let rref result of evaluating assignmentexpression.
[...]
you can see left hand side expression evaluated first, right hand side expression (assignmentexpression).
Comments
Post a Comment