javascript - 使用打字稿声明类私有变量时是否可以使用解构?
问题描述
通常,当多个变量来自同一个对象时,我会使用
const [ foo, bar, foobar ] = [ 'foo', 'bar', 'foobar' ]
但是在类型脚本中,诸如
class Test {
private {a,b,c} = tool;
}
它不再起作用了。
还有其他方法吗?
解决方案
您可以使用Object.assign
来获得相同的结果...
class Test {
private a: number;
private b: string;
private c: boolean;
constructor(arg: { a: number, b: string, c: boolean}) {
Object.assign(this, arg);
}
}
const tool = {
a: 5,
b: 'str',
c: true
}
const test = new Test(tool);
console.log(JSON.stringify(tool));
解构参数的功能仍在GitHub 上的讨论中活跃。
推荐阅读
- python - 将 2021 年 1 月 2 日转换为 2021 年 2 月 1 日
- sql - 可以在一定大小下自动实现结果集吗?
- sql - 如何一起使用 WHERE 和 INNER JOIN
- twilio - 使用 Twilio Flex 时检索电话号码
- python - 默认文本 tkinter
- java - 如何编译和运行具有多个文件/类的程序 (Mac)?
- javascript - NextJS getServerSideProps() 具有多个获取请求
- reactjs - Requests.js:7 GET http://localhost:3001/testing net::ERR_CONNECTION_REFUSED。Apache 服务器, Express, React App, Fetching , Hosting
- javascript - React prop 出现但未定义函数调用?
- ios - SwiftUI Picker 专注于视图中的选定项目