函数主要定义行为的
匿名参数
const mingz = function 无名字(参数:数值类型):返回值的类型
接口函数:
type(关键字) 别名 = (参数:number)=>返回值的类型
都需要满足接口函数的条件的
函数的四种基本形式
export default {}// 匿名函数const mim = function (a1: number, b1: number): number {return a1 + b1}let sum = mim(2, 3)// console.log(sum);// 有名函数function ming(a2: string) {return a2}let fanh = ming("十")console.log(`每天要学习${fanh}个小时`);// 箭头函数// void 没有返回值const ming1 = (a3: string, b3: number): void => {console.log(a3, b3);// return 1}ming1("十个", 10)// 接口函数type ming2 = (a4: number, b4: number) => numberconst sum1: ming2 = (x: number, y: number) => {return x + y}let fan = sum1(3, 6)console.log(fan);
函数参数的处理
可选参数
export default {}// 可选参数const fn1 = function (a: number, b?: number) {console.log(a);console.log(b);}fn1(20) //可选fn1(20,100) fn1(20,undefined)
函数默认参数 (通过默认值来控制传递参数的数量)
// 参数的默认值const fn2 = function (a1: number = 100, b1: number = 200) {console.log(a1);console.log(b1);}fn2(200, 100);fn2();
函数的剩余参数
// 函数的剩余参数const fn3 = function (a2: number, b2: number, ...c2: any[]) {console.log(a2);console.log(b2);console.log(c2);}fn3(100, 200, "张三", "赵四", true)
构造函数()
var ming = new Function("数值1" , "数值2" , "return 数值1*数值2")
export default {}// 构造函数 var myfun = new Function("a", "b", "return a*b")var res = myfun(10, 20);console.log(res);
函数重(chong)载
名字相同 ,参数数量不同 ,类型可以相同也可以不相同
参数的数量不同
export default {}//名字相同 ,参数数量不同 ,类型可以相同也可以不相同 //参数的数量不同function star(a1: string): stringfunction star(a1: string, a2: number): numberfunction star(a1: any, a2?: any): any {console.log(a1);console.log(a2);}// star("张三");star("张三", 34)
参数的类型不同
export default {}//名字相同 ,参数数量不同 ,类型可以相同也可以不相同 // 参数的类型不同function fn(a: number, b: number): numberfunction fn(a: string, b: string): stringfunction fn(a: number, b: string): stringfunction fn(a: string, b: number): stringfunction fn(a: any, b: any): any {console.log(a, b);}fn("sss", "sss")fn(2, 3)fn(4, "ss")fn("sss", 54)
函数 函数的基本使用 函数的四种基本形式 可选参数 函数默认参数 函数的剩余参数 构造函数() 函数重(chong)载
如果觉得《函数 函数的基本使用 函数的四种基本形式 可选参数 函数默认参数 函数的剩余参数》对你有帮助,请点赞、收藏,并留下你的观点哦!