ts rest and spread

typescript的新特性 三个点... 用法主要两个方面

第一方面 定义函数的时候,代表不限个数的参数

function func(...args) {
    args.forEach(function (arg) { 
        console.log(arg)
    })
}

func(1, 2, 3);

func(10,11,12,13,14)

数据结果如下,可以看到所有送参都可以被正确输出

第二种用法 调用函数的时候

function func(a, b, c) { 
    console.log(a);
    console.log(b);
    console.log(c);
}
var args = [1, 2];

func(...args);

var args = [7, 8, 9, 10, 11]

func(...args);

自动取出数组中前几个 要求几个 取出几个 打印结果如下

猜你喜欢

转载自blog.csdn.net/lee727n/article/details/107409010
ts