我正在编写一个智能合约并希望使用数组来操作数据,但是查看 AssemblyScript 文档,我不确定最好的方法。
对我来说似乎很好,只需使用:
let testData:string[] = []
但是当我查阅汇编脚本文档时,推荐了三种创建数组的方法:
// The Array constructor implicitly sets `.length = 10`, leading to an array of
// ten times `null` not matching the value type `string`. So, this will error:
var arr = new Array<string>(10);
// arr.length == 10 -> ERROR
// To account for this, the .create method has been introduced that initializes
// the backing capacity normally but leaves `.length = 0`. So, this will work:
var arr = Array.create<string>(10);
// arr.length == 0 -> OK
// When pushing to the latter array or subsequently inserting elements into it,
// .length will automatically grow just like one would expect, with the backing
// buffer already properly sized (no resize will occur). So, this is fine:
for (let i = 0; i < 10; ++i) arr[i] = "notnull";
// arr.length == 10 -> OK
我什么时候想使用一种类型的实例化而不是另一种类型的实例化?为什么我不总是使用我一开始介绍的版本?