javascript - ES6 destructuring, dynamic assignment -
this question has answer here:
- object destructuring without var 2 answers
let text, value; if (typeof f == 'string') { text = value = f; } else { let { text, value } = f; }
doing creates 2 new vars (from else
), if write so:
let text, value; if (typeof f == 'string') { text = value = f; } else { { text, value } = f; }
i receive syntax error. best approach here?
you need parens around assignment:
let text, value; if (typeof f == 'string') { text = value = f; } else { ({ // ( @ start text, value } = f); // ) @ end }
you need parens same reason you need parens or similar invoke function: tell parser should expect expression, not statement. without parens, when encounters {
, thinks that's beginning of block. unlike function, has parens, not leading unary +
, !
, etc. like this:
let text, value; if (typeof f == 'string') { text = value = f; } else { +{ // <== doesn't work iifes text, value } = f; }
Comments
Post a Comment