TypeScript 函数-可选和默认参数
//可选参数 function vvvv(first: string, second: string) { return (first + ":" + second); } let res1 = vvvv("aaa", "aaa");//right //参数个数问题 // let res2 = vvv("aa");//error // let res3 = vvv("aa","bbb","sss");//error //当前不确定传的参数是一个还是两个(不可超过参数总个数) function vvvvx(first: string, second?: string) { // return (first + ":" + second); if (second == null) { return first; } else { return (first + ":" + second); } } let res4 = vvvvx("aaa"); let res5 = vvvvx("aaa", "sss"); alert(res4 + ":::::::::::" + res5); //默认参数 给参数直接设置一个默认值 // function vvvvvx(first: string, second = "aaa") { return (first + ":" + second); } let res10 = vvvvvx("bbb");//默认第二个参数为aaa let res11 = vvvvvx("bbb","cc");//默认第二个参数为cc //let res12 = vvvvvx("bbb","aaa","ccc");//error // document.getElementById("pid").innerHTML = res10+":"+res11; alert(res10 + ":::::::::::" + res11);
TypeScript 函数-可变参数
//可选参数或者既定参数,个数都是一定的,在可变参数的rest parameter 可以不受参数个数的限制 类型受限 //...second: string[] 可变参数 function vvvv(first: string, ...second: string[]) { return (first + ":" + second.join("_")); }