ts中的函数
函数的声明是通过关键字
function
声明的
函数声明
function lee() {
console.log('乐编码')
}
lee()
函数的参数
函数的参数可以指定参数的类型
function lee1(title: string) {
console.log(title)
}
代码自行推断参数的类型
function lee2(title) {
console.log(<string>title)
}
lee2('乐编码')
我们这里断言函数的参数类型为 string
匿名函数
let lee3 = () => {
console.log('匿名参数')
}
lee3()
函数的可变参数
function lee4(...title: string[]) {
console.log(title)
}
lee4('乐编码', '作者', '寒云')
运行结果
[ '乐编码', '作者', '寒云' ]
函数的返回值
function lee5(title: string): string {
return title
}
lee5('乐编码')
函数的返回值可以是:基本数据类型,接口。如果void可以不写返回值
示例:返回值是个接口
interface leeI {
title: string
}
function lee6(title: string): leeI {
return { title: title }
}
console.log(lee6('乐编码'))