最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Multiple declarations of same variable name with object destructuring - Stack Overflow

programmeradmin0浏览0评论

I have this situation where I want to use the same variable name multiple times with object desctructuring:

let {body, response} = await requestp('get', `${cdtAPIUrl}/whitelist`, headers);
let parsedBody = await siamese.parse(body);
assert(parsedBody.success, 'response body should have a success property.');
assert(parsedBody.success.length === usernames.length, 'wrong number of items in response body array.');
let {body, response} = await requestp('get', `${cdtAPIUrl}/caches/whitelist`, headers);
let parsedBody = await siamese.parse(body);

but when I run the script with node, node will plain before runtime:

SyntaxError: Identifier 'body' has already been declared

there are two problems which prevent me from getting an easy solution:

  1. I can't do this with object destructuring:

    let {a,b} = c; {a,b} = c; // not allowed

  2. I can't rename body or response, because this is what is returned by the call.

What should I do?

Maybe the best thing to do is something like:

let {body,response} = ...
let {body:body1, response:resp1} = ...

I have this situation where I want to use the same variable name multiple times with object desctructuring:

let {body, response} = await requestp('get', `${cdtAPIUrl}/whitelist`, headers);
let parsedBody = await siamese.parse(body);
assert(parsedBody.success, 'response body should have a success property.');
assert(parsedBody.success.length === usernames.length, 'wrong number of items in response body array.');
let {body, response} = await requestp('get', `${cdtAPIUrl}/caches/whitelist`, headers);
let parsedBody = await siamese.parse(body);

but when I run the script with node, node will plain before runtime:

SyntaxError: Identifier 'body' has already been declared

there are two problems which prevent me from getting an easy solution:

  1. I can't do this with object destructuring:

    let {a,b} = c; {a,b} = c; // not allowed

  2. I can't rename body or response, because this is what is returned by the call.

What should I do?

Maybe the best thing to do is something like:

let {body,response} = ...
let {body:body1, response:resp1} = ...
Share Improve this question edited Aug 29, 2017 at 19:02 Alexander Mills asked Aug 29, 2017 at 6:26 Alexander MillsAlexander Mills 101k166 gold badges539 silver badges919 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

I can't do this with object destructuring: {a,b} = c;

You can, you just need to put it in parenthesis to be syntactically valid:

({a, b} = c);

Maybe the best thing to do is something like let {body:body2, response:resp2} = …

Yes, that's the best solution indeed. You might even want to use const instead of let.

What should I do?

Another solution, which I wouldn't necessarily remend but just want to mention for pleteness, is to introduce separate scopes for the variables:

{
  let {body, response} = await requestp('get', `${cdtAPIUrl}/whitelist`, headers);
  let parsedBody = await siamese.parse(body);
  assert(parsedBody.success, 'response body should have a success property.');
  assert(parsedBody.success.length === usernames.length, 'wrong number of items in response body array.');
}
{
  let {body, response} = await requestp('get', `${cdtAPIUrl}/caches/whitelist`, headers);
  let parsedBody = await siamese.parse(body);
}

And of course you can also just use var instead of let, which doesn't bitch about redeclarations.

发布评论

评论列表(0)

  1. 暂无评论