Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何对类范围内已声明的变量执行javascript对象的非结构化赋值_Javascript_Class_Object_This_Destructuring - Fatal编程技术网

如何对类范围内已声明的变量执行javascript对象的非结构化赋值

如何对类范围内已声明的变量执行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

我需要您的帮助,因为我正在尝试在类的作用域内声明的变量中获取javascript对象的解构赋值。。。i、 例如,我需要使用
{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想要的属性-例如应排除
百分比依赖项