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.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“不可预测”呢?)
ask by Macilias translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…