如何对类范围内已声明的变量执行javascript对象的非结构化赋值
我需要您的帮助,因为我正在尝试在类的作用域内声明的变量中获取javascript对象的解构赋值。。。i、 例如,我需要使用如何对类范围内已声明的变量执行javascript对象的非结构化赋值,javascript,class,object,this,destructuring,Javascript,Class,Object,This,Destructuring,我需要您的帮助,因为我正在尝试在类的作用域内声明的变量中获取javascript对象的解构赋值。。。i、 例如,我需要使用{this.property,…} ({ this.baseINSS, this.baseIRPF, this.discountINSS, this.dependentesDiscount, this.discountIRPF, this.netSalary } = calculateSalaryFrom(element.target.value, Num
{this.property,…}
({ this.baseINSS,
this.baseIRPF,
this.discountINSS,
this.dependentesDiscount,
this.discountIRPF,
this.netSalary } = calculateSalaryFrom(element.target.value, Number(dependentes)));
上述函数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"}
我正在获取预期的错误TS1005:':。({this.baseINSS,…
顺便说一句,我正在使用angular和typescript无冒号的对象分解只能用于有效的独立标识符
({ 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];
}
…或者只使用
对象。分配
。这将附加所有属性,而不仅仅是OP想要的属性-例如应排除百分比依赖项