gpt4 book ai didi

Javascript 通过解构重新分配 let 变量

转载 作者:行者123 更新时间:2023-12-02 19:22:04 26 4
gpt4 key购买 nike

在我的React应用程序中,我使用airbnb的eslint样式指南,如果我不使用解构,它将抛出错误。

在下面的情况下,我首先使用let分配两个变量 latitudelongitude位置对象数组中第一项的坐标。然后,如果用户允许我访问他们的位置,我会尝试使用解构来重新分配它们的值。

let latitude = locations[0].coordinates[1];
let longitude = locations[0].coordinates[0];

if (props.userLocation.coords) {
// doesn't work - unexpected token
{ latitude, longitude } = props.userLocation.coords;

// causes linting errors
// latitude = props.userLocation.coords.latitude;
// longitude = props.userLocation.coords.longitude;
}

if 内部的解构语句导致 unexpected token错误。

以老式方式重新分配变量会导致 ESlint: Use object destructuring错误。

最佳答案

 ({ latitude, longitude } = props.userLocation.coords);

解构需要在 letconstvar 声明之后进行,或者需要位于表达式上下文中以将其与 block 语句。

关于Javascript 通过解构重新分配 let 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48714689/

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