The problem of variable structure assignment and let in ES6

let x;
{x} = {x: 1};

Why is this reported wrong?

May.07,2021

x has been defined as undefined


the above code will be misspelled because the JavaScript engine interprets {x} as a block of code, resulting in a syntax error. This problem can only be solved by not writing curly braces at the beginning of the line and preventing JavaScript from interpreting it as a code block. reference

    // 
    let x;
    ({x} = {x: 1});

from teacher Ruan Yifeng's explanation of "introduction to ES6 Standards" the code writing on page 23
will report an error because the js engine will interpret {x} as a code block, resulting in a syntax error. This problem can be solved only if does not write curly braces at the beginning of the line, preventing js from interpreting it as a code block.

({x} = {x: 1}) 

that's fine

Menu