Javascript 使用解构重新分配 let 变量
Javascript re-assign let variable with destructuring
在我的 React 应用程序中,我使用的是 airbnb 的 eslint 样式指南,如果我不使用解构,它将引发错误。
在下面的情况下,我首先使用let
将两个变量latitude
和longitude
分配给location 对象数组中第一项的坐标。然后,如果用户允许我访问他们的位置,我会尝试使用解构来重新分配他们的值。
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);
解构需要在 let
、const
或 var
声明之后,或者需要在表达式上下文中以将其与块语句区分开来。
在我的 React 应用程序中,我使用的是 airbnb 的 eslint 样式指南,如果我不使用解构,它将引发错误。
在下面的情况下,我首先使用let
将两个变量latitude
和longitude
分配给location 对象数组中第一项的坐标。然后,如果用户允许我访问他们的位置,我会尝试使用解构来重新分配他们的值。
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);
解构需要在 let
、const
或 var
声明之后,或者需要在表达式上下文中以将其与块语句区分开来。