Javascript通过解构重新分配let变量
在我的React应用程序中,我使用的是airbnb的eslint样式指南,如果我不使用析构函数,它将抛出一个错误 在下面的情况中,我首先使用Javascript通过解构重新分配let变量,javascript,ecmascript-6,eslint,Javascript,Ecmascript 6,Eslint,在我的React应用程序中,我使用的是airbnb的eslint样式指南,如果我不使用析构函数,它将抛出一个错误 在下面的情况中,我首先使用let将两个变量纬度和经度分配给位置对象数组中第一项的坐标。然后,如果用户允许我访问它们的位置,我尝试使用解构来重新分配它们的值 let latitude = locations[0].coordinates[1]; let longitude = locations[0].coordinates[0]; if (props.userLocation.co
let
将两个变量纬度
和经度
分配给位置对象数组中第一项的坐标。然后,如果用户允许我访问它们的位置,我尝试使用解构来重新分配它们的值
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
语句中进行分解会导致意外标记
错误
({ latitude, longitude } = props.userLocation.coords);
以旧式方式重新分配变量会导致ESlint:Use object destructuring
错误
({ latitude, longitude } = props.userLocation.coords);
解构需要在
let
、const
或var
声明之后进行,或者需要在表达式上下文中进行,以将其与block语句区分开来。以前从未见过这种情况,它非常适合干净的try catch
块,其中函数返回一个对象。