js中的”类数组”类型

1.arguments

  1. function test() {
  2. console.log(arguments);
  3. console.log(arguments.length);
  4. }

2.HTMLAllCollection

  1. let htmlCollection: HTMLAllCollection;

3.others…

数组

  1. let arrOfNumbers: number[] = [1, 2, 3];
  2. arrOfNumbers.push(5);

元祖(tuple)

元祖的定义

  1. let user: [name: string, age: number] = ["Felix", 18];
  2. let player: [string, number] = ["Felix", 18];

元祖的本质

元祖的本质其实就是数组, 只是约束了每一个元素的类型而已, 元素的类型不是全部一样, 如果全部一样的话, 那就是数组了

  1. let user: [name: string, age: number] = ["Felix", 18];
  2. let player: [string, number] = ["Felix", 18];
  3. player.push("Chan");
  4. console.log("player", player);

(只能push元祖包含的类型, string和number)
输出结果
image.png

约束数组里面的值的类型有2种写法

  1. let a: string[] = ["a", "b", "c"];
  2. let b: Array<string> = ["a", "b", "c"];
  3. // 这种写法是约束这个数组里面只有一个元素, 并且这个元素是string
  4. let c: [string] = ["a"];