image.png参考网址:https://www.tslang.cn/docs/handbook/functions.html 剩余参数
- 源码
- Function.ts
//...restOfname:string[] 这个写法就是可变参数的写法
function peopleName(firstName:string,...restOfname:string[]){
return firstName + " " + restOfname.join(" ");
}
let pn1 = peopleName('xiaochuan','xiaoming','xiaohong');//编译成功
let pn2 = peopleName('xiaochuan','xiaoming','xiaohong','xiaoxin');//编译成功
document.getElementById('pid').innerHTML = pn1 + "<br/>" + pn2;
- index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>TypeScript 函数-可变参数</title>
</head>
<body>
<p id="pid"></p>
<script type="text/javascript" src="Functions.js"></script>
</body>
</html>
-
效果图
image.png
网友评论