admin管理员组文章数量:1023554
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:
I can't do this with object destructuring:
let {a,b} = c;
{a,b} = c; // not allowed
I can't rename
body
orresponse
, 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:
I can't do this with object destructuring:
let {a,b} = c;
{a,b} = c; // not allowed
I can't rename
body
orresponse
, 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
1 Answer
Reset to default 9I 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.
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:
I can't do this with object destructuring:
let {a,b} = c;
{a,b} = c; // not allowed
I can't rename
body
orresponse
, 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:
I can't do this with object destructuring:
let {a,b} = c;
{a,b} = c; // not allowed
I can't rename
body
orresponse
, 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
1 Answer
Reset to default 9I 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.
本文标签: javascriptMultiple declarations of same variable name with object destructuringStack Overflow
版权声明:本文标题:javascript - Multiple declarations of same variable name with object destructuring - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745597553a2158261.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论