简体   繁体   English

数组解构并分配给类字段值?

[英]Array destructuring and assign to a class field values?

class ALU {
  constructor (x, y) {
    this.zx = '0' // Zero the x input
    this.nx = '0' // Negate the x input
    this.zy = '0' // Zero the y input
    this.ny = '0' // Negate the y input
    this.f = '0' // Function code: 1 for Add, 0 for And
    this.no = '0' // Negate the output
  }

  ...

  setControlBits (controlBits) {
    this.zx = controlBits[0]
    this.nx = controlBits[1]
    this.zy = controlBits[2]
    this.ny = controlBits[3]
    this.f = controlBits[4]
    this.no = controlBits[5]
  }

  ...
}

How to write this code in a succinct way?如何以简洁的方式编写此代码? Like can we destructure the array and assign it to the class field values?比如我们可以解构数组并将其分配给类字段值吗?

Try this one.试试这个。

 function Foo(list) { [this.x, this.y] = list; } foo = new Foo([1, 2]); console.log(foo.x); console.log(foo.y);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM