为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

胡说叔叔 2023-08-24 17:52:23
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]  }  ...}如何简洁地编写这段代码?就像我们可以解构数组并将其分配给类字段值吗?
查看完整描述

1 回答

?
九州编程

TA贡献1785条经验 获得超4个赞

试试这个。


function Foo(list) {

    [this.x, this.y] = list;

}


foo = new Foo([1, 2]);


console.log(foo.x);

console.log(foo.y);


查看完整回答
反对 回复 2023-08-24
  • 1 回答
  • 0 关注
  • 82 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信