gpt4 book ai didi

javascript - 重用通过解构创建的对象字面量

转载 作者:行者123 更新时间:2023-11-30 11:35:47 26 4
gpt4 key购买 nike

我正在尝试为两个异步调用重用对象文字。最后,我的期望应该检查 deleteBucket 调用是否成功。问题是我不能这样做,或者它说我已经定义了重复变量:

it('can delete a bucket', async () => {
const options = { branch: '11' }

let { failure, success, payload } = await deployApi.createBucket(options)
let { failure, success, payload} = await deployApi.deleteBucket(options.branch)

expect(success).to.be.true
})

有人告诉我我可以在第二个附近放一个 () ,但这给了我一个 TypeError: (0 , _context4.t0) is not a function 错误:

it('can delete a bucket', async () => {
const options = { branch: '11' }

let { failure, success, payload } = await deployApi.createBucket(options)

({ failure, success, payload} = await deployApi.deleteBucket(options.branch))

expect(success).to.be.true
})

这确实有效,但需要我更改我不想做的已解析对象的名称:

it('can delete a bucket', async () => {
const options = { branch: '11' }

let { failure, success, payload } = await deployApi.createBucket(options)
let { failure1, success1, payload1} = await deployApi.deleteBucket(options.branch)

expect(success1).to.be.true
})

更新:

有人建议我在 const 行之后需要一个分号。没有任何区别,当我运行它时我仍然得到同样的错误:

enter image description here

最佳答案

缺少分号

你有两个 (...) 排列在一起 -

await deployApi.createBucket(options)  

({ failure, success, payload} = await deployApi.deleteBucket(options.branch))

ES 解释器将其视为 -

await deployApi.createBucket(options)(... await deployApi.deleteBucket(options.branch))

相当于-

const r1 = deployApi.createBucket(options)
const r2 = r1(... await deployApi.deleteBucket(options.branch))
await r2

这与您的实际意图非常不同 -

const r1 = await deployApi.createBucket(...)
const r2 = await deployApi.deleteBucket(...)

要重用let解构对象,需要括号-

// initial assignment
let { a, b, c } = ...

// without the parentheses, ES interprets as illegal assignment using =
{ a, b, c } = ...

// with parentheses, ES interprets as destructuring assignment
({ a, b, c } = ...)

如果您重复使用相同的 let 绑定(bind),则在不使用分号时,必需的 括号会改变程序的含义。

it('can delete a bucket', async () => {
const options = { branch: '11' }
// semicolon here
let { failure, success, payload } = await deployApi.createBucket(options);

({ failure, success, payload} = await deployApi.deleteBucket(options.branch))

expect(success).to.be.true
})

关于javascript - 重用通过解构创建的对象字面量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44469366/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com