javascript - 数组解构并分配给类字段值?
问题描述
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]
}
...
}
如何以简洁的方式编写此代码?就像我们可以解构数组并将其分配给类字段值吗?
解决方案
试试这个。
function Foo(list) {
[this.x, this.y] = list;
}
foo = new Foo([1, 2]);
console.log(foo.x);
console.log(foo.y);
推荐阅读
- java - 安装新的 eclipse 更新后收到错误消息
- d3.js - D3 如何修复工具提示事件问题?
- flutter - pub 获取失败(1;可运行的程序或批处理文件。)
- amazon-web-services - 计算优化实例和加速计算实例的区别
- gstreamer - GStreamer 的 rtspsrc 元素可以通过 DESCRIBE 请求的身份验证标头吗?
- reactjs - 带有打字稿的formik在withFormik HOC中设置了初始值
- javascript - npm install 后,依赖项被忽略并从 package-lock.json 中删除
- python - 熊猫应用有条件的功能
- reactjs - 从 Firebase 实时数据库中获取当前值的键
- python - 如何将嵌入广播到不同的服务器不和谐 python