简体   繁体   English

类型错误:class.function 不是函数。 (在'classname.function(param)'中)

[英]TypeError: class.function is not a function. (In 'classname.function(param)')

I'm not an expert to JavaScript, but following the logic of languages like Java and others a the following should work, but does not:我不是 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();

when calling addNewAssets I would expect to get merged assets as the result, but the result is:当调用 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)

Does it have anything to do with JS "unpredictable" this ?是否有任何与JS“不可预测”呢

Even when some very smart folks has downgraded that question, it is showcasing a pitfall with JS classes and Flow.即使一些非常聪明的人降级了这个问题,它也显示了 JS 类和 Flow 的一个陷阱。

Since the assets has been used after being read back from JSON some class information has get lost, even tho the fields has been typed, Flow is not Java and not hard typed.由于资产在从 JSON 读回后已被使用,因此某些类信息丢失了,即使字段已被键入,Flow 不是 Java 也不是硬类型。

I'll need to try this: https://github.com/STRML/json-to-flow or https://www.npmjs.com/package/flow-validator as described here: How can I get JSON.parse result covered by the Flow type checker?我需要试试这个: https : //github.com/STRML/json-to-flowhttps://www.npmjs.com/package/flow-validator如下所述: How can I get JSON.parse Flow 类型检查器覆盖的结果?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:默认不是函数。 导入,导出JavaScript类 - TypeError: default is not a function. Import, export JavaScript class TypeError:未定义不是函数。 iOS Safari评估功能。 CoffeeScript - TypeError: Undefined is not a function. iOS Safari Evaluating function. CoffeeScript 获取“TypeError:res.status 不是 function。” - Getting "TypeError: res.status is not a function." 类型错误:defineCall 不是 function。 要求()失败 - TypeError: defineCall is not a function. require() fails className不是函数 - className is not a function 如何为.push函数制作数组。 收到“ typeError:.push不是函数”? - How to make array for a .push function. Getting “typeError: .push is not a function”? Angular,将泛型类型传递给另一个函数。 如何输入参数? - Angular, pass generic type into another function. How to typed param? WordPress管理区域-TypeError:$不是函数。 (在“ $(“。tab_content”)”中,“ $”未定义) - Wordpress Admin area - TypeError: $ is not a function. (In '$(“.tab_content”)', '$' is undefined) jQuery第67行说“TypeError:&#39;undefined&#39;不是函数。” - jQuery line 67 saying “TypeError: 'undefined' is not a function.” 未捕获的TypeError:$(...)。data(...)。sqaveAsPDF不是函数。 剑道网格 - Uncaught TypeError: $(…).data(…).saveAsPDF is not a function. kendo Grid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM