TypeError:class.function不是函数。 (在'classname.function(param)'中)

问题描述 投票:-3回答:1

我不是JavaScript的专家,但是遵循Java和其他语言的逻辑,以下应该可以,但不能:

//* @flow */

class Assets {
  parts: Array<PartsItem>;

  constructor() {
    this.parts = [];
  }

  merge(other: Assets) {
    this.parts = this.parts.concat(other.parts);
  }
}

class PartsItem {
  name: string;
}

function addNewAssets(assets: Assets, newAssets: Assets) {
  // the out log verifies that both assets are having all expected values and expected structure
  console.log("adding ", newAssets);
  console.log("to ", assets);
  assets.merge(newAssets);
  return assets;
}

function run_example() {
  let item1: PartsItem = new PartsItem();
  item1.name = "part1";
  let item2: PartsItem = new PartsItem();
  item2.name = "part2";

  let assets = new Assets();
  let otherAssets = new Assets();

  assets.parts.push(item1);
  otherAssets.parts.push(item1);

  let mergedAssets = addNewAssets(assets, otherAssets);

  console.log(JSON.stringify(mergedAssets));
}

run_example();

当调用addNewAssets时,我希望得到合并的资产,但结果是:

[12:06:55] W | ReactNativeJS ▶︎ Possible Unhandled Promise Rejection (id: 0):
                             │ TypeError: assets.merge is not a function. (In 'assets.merge(newAssets)', 'assets.merge' is undefined)

与JS“ unpredictable” this有关系吗?

javascript reactjs typescript flowtype ecmascript-5
1个回答
1
投票

即使有些非常聪明的人

© www.soinside.com 2019 - 2024. All rights reserved.