99``` js
1010// 函数声明(Function Declaration)
1111function sum (x , y ) {
12- return x + y;
12+ return x + y;
1313}
1414
1515// 函数表达式(Function Expression)
1616let mySum = function (x , y ) {
17- return x + y;
17+ return x + y;
1818};
1919```
2020
2121一个函数有输入和输出,要在 TypeScript 中对其进行约束,需要把输入和输出都考虑到,其中函数声明的类型定义较简单:
2222
2323``` ts
2424function sum(x : number , y : number ): number {
25- return x + y ;
25+ return x + y ;
2626}
2727```
2828
2929注意,** 输入多余的(或者少于要求的)参数,是不被允许的** :
3030
3131``` ts
3232function sum(x : number , y : number ): number {
33- return x + y ;
33+ return x + y ;
3434}
3535sum (1 , 2 , 3 );
3636
@@ -39,7 +39,7 @@ sum(1, 2, 3);
3939
4040``` ts
4141function sum(x : number , y : number ): number {
42- return x + y ;
42+ return x + y ;
4343}
4444sum (1 );
4545
@@ -52,34 +52,36 @@ sum(1);
5252
5353``` ts
5454let mySum = function (x : number , y : number ): number {
55- return x + y ;
55+ return x + y ;
5656};
5757```
5858
5959这是可以通过编译的,不过事实上,上面的代码只对等号右侧的匿名函数进行了类型定义,而等号左边的 ` mySum ` ,是通过赋值操作进行类型推论而推断出来的。如果需要我们手动给 ` mySum ` 添加类型,则应该是这样:
6060
6161``` ts
6262let mySum: (x : number , y : number ) => number = function (x : number , y : number ): number {
63- return x + y ;
63+ return x + y ;
6464};
6565```
6666
67+ 注意不要混淆了 TypeScript 中的 ` => ` 和 ES6 中的 ` => ` 。
68+
6769在 TypeScript 的类型定义中,` => ` 用来表示函数的定义,左边是输入类型,需要用括号括起来,右边是输出类型。
6870
69- 其中 ,` => ` 在 ES6 中叫箭头函数 ,应用十分广泛,可以参考 [ ES6 中的箭头函数] [ ] 。
71+ 在 ES6 中 ,` => ` 叫做箭头函数 ,应用十分广泛,可以参考 [ ES6 中的箭头函数] [ ] 。
7072
71- ## 接口中函数的定义
73+ ## 用接口定义函数的形状
7274
7375我们也可以使用接口的方式来定义一个函数需要符合的形状:
7476
7577``` ts
7678interface SearchFunc {
77- (source : string , subString : string ): boolean ;
79+ (source : string , subString : string ): boolean ;
7880}
7981
8082let mySearch: SearchFunc ;
8183mySearch = function (source : string , subString : string ) {
82- return source .search (subString ) !== - 1 ;
84+ return source .search (subString ) !== - 1 ;
8385}
8486```
8587
@@ -91,52 +93,52 @@ mySearch = function(source: string, subString: string) {
9193
9294``` ts
9395function buildName(firstName : string , lastName ? : string ) {
94- if (lastName ) {
95- return firstName + ' ' + lastName ;
96- } else {
97- return firstName ;
98- }
96+ if (lastName ) {
97+ return firstName + ' ' + lastName ;
98+ } else {
99+ return firstName ;
100+ }
99101}
100- let xcatliu = buildName (' Xcat ' , ' Liu ' );
101- let xcat = buildName (' Xcat ' );
102+ let tomcat = buildName (' Tom ' , ' Cat ' );
103+ let tom = buildName (' Tom ' );
102104```
103105
104106需要注意的是,可选参数必须接在必需参数后面。换句话说,** 可选参数后面不允许再出现必须参数了** :
105107
106108``` ts
107109function buildName(firstName ? : string , lastName : string ) {
108- if (firstName ) {
109- return firstName + ' ' + lastName ;
110- } else {
111- return lastName ;
112- }
110+ if (firstName ) {
111+ return firstName + ' ' + lastName ;
112+ } else {
113+ return lastName ;
114+ }
113115}
114- let xcatliu = buildName (' Xcat ' , ' Liu ' );
115- let xcat = buildName (undefined , ' Xcat ' );
116+ let tomcat = buildName (' Tom ' , ' Cat ' );
117+ let tom = buildName (undefined , ' Tom ' );
116118
117119// index.ts(1,40): error TS1016: A required parameter cannot follow an optional parameter.
118120```
119121
120122## 参数默认值
121123
122- 在 ES6 中,我们允许给函数的参数添加默认值,TypeScript 会将 ** 添加了默认值的参数识别为可选参数 ** :
124+ 在 ES6 中,我们允许给函数的参数添加默认值,** TypeScript 会将添加了默认值的参数识别为可选参数 ** :
123125
124126``` ts
125- function buildName(firstName : string , lastName : string = ' Liu ' ) {
126- return firstName + ' ' + lastName ;
127+ function buildName(firstName : string , lastName : string = ' Cat ' ) {
128+ return firstName + ' ' + lastName ;
127129}
128- let xcatliu = buildName (' Xcat ' , ' Liu ' );
129- let xcat = buildName (' Xcat ' );
130+ let tomcat = buildName (' Tom ' , ' Cat ' );
131+ let tom = buildName (' Tom ' );
130132```
131133
132134此时就不受「可选参数必须接在必需参数后面」的限制了:
133135
134136``` ts
135- function buildName(firstName : string = ' Xcat ' , lastName : string ) {
136- return firstName + ' ' + lastName ;
137+ function buildName(firstName : string = ' Tom ' , lastName : string ) {
138+ return firstName + ' ' + lastName ;
137139}
138- let xcatliu = buildName (' Xcat ' , ' Liu ' );
139- let xcat = buildName (undefined , ' Xcat ' );
140+ let tomcat = buildName (' Tom ' , ' Cat ' );
141+ let cat = buildName (undefined , ' Cat ' );
140142```
141143
142144> 关于默认参数,可以参考 [ ES6 中函数参数的默认值] [ ] 。
@@ -147,9 +149,9 @@ ES6 中,可以使用 `...rest` 的方式获取函数中的剩余参数(rest
147149
148150``` js
149151function push (array , ... items ) {
150- items .forEach (function (item ) {
151- array .push (item);
152- });
152+ items .forEach (function (item ) {
153+ array .push (item);
154+ });
153155}
154156
155157let a = [];
@@ -160,9 +162,9 @@ push(a, 1, 2, 3);
160162
161163``` ts
162164function push(array : any [], ... items : any []) {
163- items .forEach (function (item ) {
164- array .push (item );
165- });
165+ items .forEach (function (item ) {
166+ array .push (item );
167+ });
166168}
167169
168170let a = [];
@@ -181,11 +183,11 @@ push(a, 1, 2, 3);
181183
182184``` ts
183185function reverse(x : number | string ): number | string {
184- if (typeof x === ' number' ) {
185- return Number (x .toString ().split (' ' ).reverse ().join (' ' ));
186- } else if (typeof x === ' string' ) {
187- return x .split (' ' ).reverse ().join (' ' );
188- }
186+ if (typeof x === ' number' ) {
187+ return Number (x .toString ().split (' ' ).reverse ().join (' ' ));
188+ } else if (typeof x === ' string' ) {
189+ return x .split (' ' ).reverse ().join (' ' );
190+ }
189191}
190192```
191193
@@ -197,11 +199,11 @@ function reverse(x: number | string): number | string {
197199function reverse(x : number ): number ;
198200function reverse(x : string ): string ;
199201function reverse(x : number | string ): number | string {
200- if (typeof x === ' number' ) {
201- return Number (x .toString ().split (' ' ).reverse ().join (' ' ));
202- } else if (typeof x === ' string' ) {
203- return x .split (' ' ).reverse ().join (' ' );
204- }
202+ if (typeof x === ' number' ) {
203+ return Number (x .toString ().split (' ' ).reverse ().join (' ' ));
204+ } else if (typeof x === ' string' ) {
205+ return x .split (' ' ).reverse ().join (' ' );
206+ }
205207}
206208```
207209
0 commit comments