[英]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, ... }
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)));
The above mentioned function calculateSalaryFrom()
will return上面提到的 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"}
I am geting the error TS1005: ':' expected.({ this.baseINSS,...
By the way, I am using angular and typescript我
error TS1005: ':' expected.({ this.baseINSS,...
Object destructuring without colons can only be done into works for valid standalone identifiers. Object 没有冒号的解构只能用于有效的独立标识符。 Eg
例如
({ this.baseINSS } = someObj);
won't work because the property of someObj
isn't this.baseINSS
- it's just baseINSS
.不起作用,因为
someObj
的属性不是this.baseINSS
- 它只是baseINSS
。
While you could rename the property as you destructure:虽然您可以在解构时重命名属性:
({ baseINSS: this.baseINSS, discountINSS: this.discountINSS } = calculateSalaryFrom(...
That'll get repetitive.那会重复。
Destructuring won't work well here.解构在这里不能很好地工作。 Iterate over an array of property names instead.
而是迭代一组属性名称。
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.