[英]How to perform a destructuring assignment of a javascript object to already declared variables inside the scope of a class
I need your help, because I am trying to get a destructuring assignment of a javascript object inside of variables already declared inside the scope of a class... ie , I need to use {this.property, ... }
({ this.baseINSS,
this.baseIRPF,
this.discountINSS,
this.dependentesDiscount,
this.discountIRPF,
this.netSalary } = calculateSalaryFrom(element.target.value, Number(dependentes)));
上面提到的 function calculateSalaryFrom()
会返回
{baseINSS: "9876",
baseIRPF: 9162.9,
dependentesDiscount: 0,
discountINSS: 713.1,
discountIRPF: 1650.44,
netSalary: 7512.459999999999,
percentageDependentes: "0.00",
percentageINSS: "7.22",
percentageIRPF: "16.71",
percentageSalary: "76.07"}
我error TS1005: ':' expected.({ this.baseINSS,...
Object 没有冒号的解构只能用于有效的独立标识符。 例如
({ this.baseINSS } = someObj);
不起作用,因为someObj
的属性不是this.baseINSS
- 它只是baseINSS
。
虽然您可以在解构时重命名属性:
({ baseINSS: this.baseINSS, discountINSS: this.discountINSS } = calculateSalaryFrom(...
那会重复。
解构在这里不能很好地工作。 而是迭代一组属性名称。
const obj = calculateSalaryFrom(element.target.value, Number(dependentes)));
const props = ['baseINSS', 'baseIRPH', ...] as const;
for (const prop of props) {
this[prop] = obj[prop];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.