用'let'解构赋值后,我不能重新分配其中的一个变量吗?

问题描述 投票:0回答:0

在练习 NestJS..

DTO 在 Controller 中使用,我通过解构赋值为它们声明了 3 个变量。 这时,我在声明测试时使用了'let'关键字。

export class SignUpDto {
  name: string;
  email: string;
  readonly password: string;
}
  @Post()
  async signUp(@Body() signUpDto: SignUpDto): Promise<void> {
    let { name, email, password } = signUpDto;
    console.log(1, name);
    if (!name || !email || !password) {
      throw new BadRequestException();
    }
    name = 'new name';
    console.log(2, name);
    console.log(signUpDto.name);
    await this.usersService.signUp(signUpDto);
  }

在声明之后,'name' 具有 REQ 正文所具有的值。('username') 所以我将其值更改为“新名称”

然后当我只记录“名称”变量时,“名称”变量在控制台中打印“新名称”。 但是,当我登录“signUpDto.name”时,它仍然打印“用户名”。

// console.log(1, name);
1 username

// console.log(2, name);
2 new name

// console.log(signUpDto.name);
username

// the result returned from usersService
UsersService { name: 'username', email: '[email protected]', password: 'PassWord' }

我看过官方文档,但我找不到正确的理由。

我的一个想法是,它们存储在内存中的不同位置,因此它们打印出不同的值。

你能告诉我这是什么意思吗? 我不知道为什么 'name's value 没有改变..

typescript nestjs destructuring
© www.soinside.com 2019 - 2024. All rights reserved.