简体   繁体   中英

Javascript destructing in Chrome console

I am trying JavaScript destructing with following code in Chrome's console tab which give me Uncaught SyntaxError: Identifier 'a' has already been declared exception

o = { a: "foo", b: 12, c: "bar" };
let { a, b } = o;
console.log(a);
console.log(b);

output:
foo
12

but just changing variable names, it runs fine, like following,

o = { p: "foo", q: 12, r: "bar" };
let { p, q } = o;
console.log(p);
console.log(q);

Can anyone explain me why is this happening ?

Well, both work ok the first time, the problem is you executed the code more than once. The next time you'll have that error because the var is already defined when you previously executed the code.

Try the following. Open a new chrome browser and type about:blank in the URL. type

let a;

in the console twice and you'll get the same error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM