TypeScript 函数
注意:参数不能同时设置为可选和默认。
实例
以下实例函数的参数 rate 设置了默认值为 0.50,调用该函数时如果未传入参数则使用该默认值:
TypeScriptfunction calculate_discount(price:number,rate:number = 0.50) {
var discount = price * rate;
console.log("计算结果: ",discount);
}
calculate_discount(1000)
calculate_discount(1000,0.30)
编译以上代码,得到以下 JavaScript 代码:
JavaScriptfunction calculate_discount(price, rate) {
if (rate === void 0) { rate = 0.50; }
var discount = price * rate;
console.log("计算结果: ", discount);
}
calculate_discount(1000);
calculate_discount(1000, 0.30);
输出结果为:
计算结果: 500
计算结果: 300
剩余参数
有一种情况,我们不知道要向函数传入多少个参数,这时候我们就可以使用剩余参数来定义。
剩余参数语法允许我们将一个不确定数量的参数作为一个数组传入。
TypeScriptfunction buildName(firstName: string, ...restOfName: string[]) {
return firstName + " " + restOfName.join(" ");
}
let employeeName = buildName("Joseph", "Samuel", "Lucas", "MacKinzie");
函数的最后一个命名参数 restOfName 以 ... 为前缀,它将成为一个由剩余参数组成的数组,索引值从0(包括)到 restOfName.length(不包括)。
TypeScriptfunction addNumbers(...nums:number[]) {
var i;
var sum:number = 0;
for(i = 0;i sum = sum + nums[i]; } console.log("和为:",sum) } addNumbers(1,2,3) addNumbers(10,10,10,10,10) 编译以上代码,得到以下 JavaScript 代码: JavaScriptfunction addNumbers() { var nums = []; for (var _i = 0; _i < arguments.length; _i++) { nums[_i] = arguments[_i]; } var i; var sum = 0; for (i = 0; i < nums.length; i++) { sum = sum + nums[i]; } console.log("和为:", sum); } addNumbers(1, 2, 3); addNumbers(10, 10, 10, 10, 10); 输出结果为: 和为: 6 和为: 50 匿名函数 匿名函数是一个没有函数名的函数。 匿名函数在程序运行时动态声明,除了没有函数名外,其他的与标准函数一样。 我们可以将匿名函数赋值给一个变量,这种表达式就成为函数表达式。 语法格式如下: var res = function( [arguments] ) { ... } 实例 不带参数匿名函数: TypeScriptvar msg = function() { return "hello world"; } console.log(msg()) 编译以上代码,得到以下 JavaScript 代码: JavaScriptvar msg = function () { return "hello world"; }; console.log(msg()); 输出结果为: hello world 带参数匿名函数: TypeScriptvar res = function(a:number,b:number) { return a*b; }; console.log(res(12,2)) 编译以上代码,得到以下 JavaScript 代码: JavaScriptvar res = function (a, b) { return a * b; }; console.log(res(12, 2)); 输出结果为: 24 匿名函数自调用 匿名函数自调用在函数后使用 () 即可: TypeScript(function () { var x = "Hello!!"; console.log(x) })() 编译以上代码,得到以下 JavaScript 代码: JavaScript(function () { var x = "Hello!!"; console.log(x) })() 输出结果为: Hello!! 构造函数 TypeScript 也支持使用 JavaScript 内置的构造函数 Function() 来定义函数: 语法格式如下: var res = new Function ([arg1[, arg2[, ...argN]],] functionBody) 参数说明: arg1, arg2, ... argN:参数列表。 functionBody:一个含有包括函数定义的 JavaScript 语句的字符串。 实例 TypeScriptvar myFunction = new Function("a", "b", "return a * b"); var x = myFunction(4, 3); console.log(x); 编译以上代码,得到以下 JavaScript 代码: JavaScriptvar myFunction = new Function("a", "b", "return a * b"); var x = myFunction(4, 3); console.log(x); 输出结果为: 12 递归函数 递归函数即在函数内调用函数本身。 举个例子: 从前有座山,山里有座庙,庙里有个老和尚,正在给小和尚讲故事呢!故事是什么呢?"从前有座山,山里有座庙,庙里有个老和尚,正在给小和尚讲故事呢!故事是什么呢?'从前有座山,山里有座庙,庙里有个老和尚,正在给小和尚讲故事呢!故事是什么呢?……'" 实例 TypeScriptfunction factorial(number) { if (number <= 0) { // 停止执行 return 1; } else { return (number * factorial(number - 1)); // 调用自身 } }; console.log(factorial(6)); // 输出 720 编译以上代码,得到以下 JavaScript 代码: JavaScriptfunction factorial(number) { if (number <= 0) { // 停止执行 return 1; } else { return (number * factorial(number - 1)); // 调用自身 } } ; console.log(factorial(6)); // 输出 720 输出结果为: 720 Lambda 函数 Lambda 函数也称之为箭头函数。 箭头函数表达式的语法比函数表达式更短。 函数只有一行语句: ( [param1, param2,…param n] )=>statement; 实例 以下实例声明了 lambda 表达式函数,函数返回两个数的和: TypeScriptvar foo = (x:number)=>10 + x console.log(foo(100)) //输出结果为 110 编译以上代码,得到以下 JavaScript 代码: JavaScriptvar foo = function (x) { return 10 + x; }; console.log(foo(100)); //输出结果为 110 输出结果为: 110 函数是一个语句块: ( [param1, param2,…param n] )=> { // 代码块 } 实例 以下实例声明了 lambda 表达式函数,函数返回两个数的和: TypeScriptvar foo = (x:number)=> { x = 10 + x console.log(x) } foo(100) 编译以上代码,得到以下 JavaScript 代码: JavaScriptvar foo = function (x) { x = 10 + x; console.log(x); }; foo(100); 输出结果为: 110 我们可以不指定函数的参数类型,通过函数内来推断参数类型: TypeScriptvar func = (x)=> { if(typeof x=="number") { console.log(x+" 是一个数字") } else if(typeof x=="string") { console.log(x+" 是一个字符串") } } func(12) func("Tom") 编译以上代码,得到以下 JavaScript 代码: JavaScriptvar func = function (x) { if (typeof x == "number") { console.log(x + " 是一个数字"); } else if (typeof x == "string") { console.log(x + " 是一个字符串"); } }; func(12); func("Tom"); 输出结果为: 12 是一个数字 Tom 是一个字符串 单个参数 () 是可选的: TypeScriptvar display = x => { console.log("输出为 "+x) } display(12) 编译以上代码,得到以下 JavaScript 代码: JavaScriptvar display = function (x) { console.log("输出为 " + x); }; display(12); 输出结果为: 输出为 12 无参数时可以设置空括号: TypeScriptvar disp =()=> { console.log("Function invoked"); } disp(); 编译以上代码,得到以下 JavaScript 代码: JavaScriptvar disp = function () { console.log("调用函数"); }; disp(); 输出结果为: 调用函数 函数重载 重载是方法名字相同,而参数不同,返回类型可以相同也可以不同。 每个重载的方法(或者构造函数)都必须有一个独一无二的参数类型列表。 参数类型不同: function disp(string):void; function disp(number):void; 参数数量不同: function disp(n1:number):void; function disp(x:number,y:number):void; 参数类型顺序不同: function disp(n1:number,s1:string):void; function disp(s:string,n:number):void; 如果参数类型不同,则参数类型应设置为 any。 参数数量不同你可以将不同的参数设置为可选。 实例 以下实例定义了参数类型与参数数量不同: TypeScriptfunction disp(s1:string):void; function disp(n1:number,s1:string):void; function disp(x:any,y?:any):void { console.log(x); console.log(y); } disp("abc") disp(1,"xyz"); 编译以上代码,得到以下 JavaScript 代码: JavaScriptfunction disp(x, y) { console.log(x); console.log(y); } disp("abc"); disp(1, "xyz"); 输出结果为: abc undefined 1 xyz
随便看看
- 2025-08-11 19:24:1406款天籁230jk真实油耗,老天籁230jk为什么费油
- 2025-08-11 17:44:51【阴阳师】妖狐最强御魂搭配推荐
- 2025-09-23 19:43:12狂野飙车8账号被封怎么解封 狂野飙车8账号能不能登9
- 2025-06-25 02:44:22如何在线将您的 MP4 转换为 JPG?
- 2025-06-29 10:40:0612 款奥迪 a6l 导航怎么用
- 2025-06-09 18:34:12成长魔方APP
- 2025-08-19 22:33:40头发为什么白
- 2025-09-28 16:32:26《王者荣耀》S29王昭君极致控场与爆发出装深度教学
- 2025-08-13 04:57:03evga和华硕显卡哪个好
- 2025-08-01 23:37:13注册域名为何要收费
