const object = this.variable的作用是什么?

时间:2018-08-21 11:15:26

标签: javascript ecmascript-6 const

我正在开发一个本机应用程序,该应用程序最初是由一家远程公司开发的。 在这个项目中,我找到了以下代码:

class SomeScreen extends Component {

  constructor {
    this.state = {
      connection: null
      codeInput: '',
      paramInput: '',
    }
  }

  someFunction() {
    const {
      connection,
      codeInput: code,
      paramInput: params,
    } = this.state
    this.otherFunction(connetion.var, parseInt(code), parseInt(params))
  }
}

我想知道const {} = this.variable的作用。我从未见过这种分配方式,我想知道它是否与

相同
this.state.code = code;
this.state.params = params;

2 个答案:

答案 0 :(得分:2)

请考虑以下代码片段以了解您的代码。基本上,您只是复制到const变量。

let obj = {a:1, b:2, c:3}
const {a} = obj;
console.log(a);
obj.a = 4;
console.log(obj.a);
console.log(a);

答案 1 :(得分:1)

这是对象分解。因此,您可以从数组中的值或对象中的属性中解包。您可以单击链接以获取更多详细信息 Object Destructuring Info

相关问题