【问题标题】:Javascript re-assign let variable with destructuring [duplicate]Javascript通过解构重新分配let变量[重复]
【发布时间】:2018-07-20 17:55:22
【问题描述】:

在我的 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 错误。

【问题讨论】:

    标签: javascript ecmascript-6 eslint


    【解决方案1】:
     ({ latitude, longitude } = props.userLocation.coords);
    

    解构需要在letconstvar 声明之后,或者需要在表达式上下文中以将其与块语句区分开来。

    【讨论】:

    猜你喜欢
    • 2019-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 2014-11-08
    相关资源
    最近更新 更多