javascript - ES6 destructuring, dynamic assignment -


this question has answer here:

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 } 

(live copy on babel.)

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

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -