[英]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-flow或https://www.npmjs.com/package/flow-validator如下所述: How can I get JSON.parse Flow 类型检查器覆盖的结果?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.