首页 > 解决方案 > 我可以在 TypeScript 中将引用作为数组项传递吗?

问题描述

我想将数组中的引用传递给 JavaScript 中的函数,如下所示:

class MyClass {
  // ...

  myfunc(['str1', 'str2'], [this.companies, this.devizas, this.paymentTypes]);

  // ...
}

inmyfunc是这样的:

function myfunc(firstArg: string[], references: any[]): void {
  const payload: any[] = [];
  // payload's content coming from an observable and length will be equal to `reference` array

  for (let i = 0; i < payload.length; i++) {
    references[i] = payload[i];
    // here I want to pass value payload[0] into the caller class this.companies field
  }
}

现在——如果我认为正确的话——以这种方式,该references[i] = payload[i];行将用一个值覆盖引用,而不是将值加载到引用中。

有没有办法在数组中传递引用并用值填充引用?

标签: typescriptreferenceparameter-passing

解决方案


推荐阅读