JavaScript报错:Invalid shorthand property initializer

JavaScript中使用JSON格式定义一个对象时

{
     
    let {
     a=10,b=20} = {
     a=30};
    console.log(a,b)
}

报错信息如下:

SyntaxError: Invalid shorthand property initializer
?[90m    at Module._compile (internal/modules/cjs/loader.js:895:18)?[39m
?[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)?[39m
?[90m    at Module.load (internal/modules/cjs/loader.js:815:32)?[39m
?[90m    at Function.Module._load (internal/modules/cjs/loader.js:727:14)?[39m
?[90m    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)?[39m
?[90m    at internal/main/run_main_module.js:17:11?[39m

解决方案:

原因是“=”应该写为“:”这个经常错,还不容易发现,所以记录下来,加强记忆

{
     
    let {
     a=10,b=20} = {
     a:30};
    console.log(a,b)
}

你可能感兴趣的:(异常处理)