[英]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.