9
9
``` js
10
10
// 函数声明(Function Declaration)
11
11
function sum (x , y ) {
12
- return x + y;
12
+ return x + y;
13
13
}
14
14
15
15
// 函数表达式(Function Expression)
16
16
let mySum = function (x , y ) {
17
- return x + y;
17
+ return x + y;
18
18
};
19
19
```
20
20
21
21
一个函数有输入和输出,要在 TypeScript 中对其进行约束,需要把输入和输出都考虑到,其中函数声明的类型定义较简单:
22
22
23
23
``` ts
24
24
function sum(x : number , y : number ): number {
25
- return x + y ;
25
+ return x + y ;
26
26
}
27
27
```
28
28
29
29
注意,** 输入多余的(或者少于要求的)参数,是不被允许的** :
30
30
31
31
``` ts
32
32
function sum(x : number , y : number ): number {
33
- return x + y ;
33
+ return x + y ;
34
34
}
35
35
sum (1 , 2 , 3 );
36
36
@@ -39,7 +39,7 @@ sum(1, 2, 3);
39
39
40
40
``` ts
41
41
function sum(x : number , y : number ): number {
42
- return x + y ;
42
+ return x + y ;
43
43
}
44
44
sum (1 );
45
45
@@ -52,34 +52,36 @@ sum(1);
52
52
53
53
``` ts
54
54
let mySum = function (x : number , y : number ): number {
55
- return x + y ;
55
+ return x + y ;
56
56
};
57
57
```
58
58
59
59
这是可以通过编译的,不过事实上,上面的代码只对等号右侧的匿名函数进行了类型定义,而等号左边的 ` mySum ` ,是通过赋值操作进行类型推论而推断出来的。如果需要我们手动给 ` mySum ` 添加类型,则应该是这样:
60
60
61
61
``` ts
62
62
let mySum: (x : number , y : number ) => number = function (x : number , y : number ): number {
63
- return x + y ;
63
+ return x + y ;
64
64
};
65
65
```
66
66
67
+ 注意不要混淆了 TypeScript 中的 ` => ` 和 ES6 中的 ` => ` 。
68
+
67
69
在 TypeScript 的类型定义中,` => ` 用来表示函数的定义,左边是输入类型,需要用括号括起来,右边是输出类型。
68
70
69
- 其中 ,` => ` 在 ES6 中叫箭头函数 ,应用十分广泛,可以参考 [ ES6 中的箭头函数] [ ] 。
71
+ 在 ES6 中 ,` => ` 叫做箭头函数 ,应用十分广泛,可以参考 [ ES6 中的箭头函数] [ ] 。
70
72
71
- ## 接口中函数的定义
73
+ ## 用接口定义函数的形状
72
74
73
75
我们也可以使用接口的方式来定义一个函数需要符合的形状:
74
76
75
77
``` ts
76
78
interface SearchFunc {
77
- (source : string , subString : string ): boolean ;
79
+ (source : string , subString : string ): boolean ;
78
80
}
79
81
80
82
let mySearch: SearchFunc ;
81
83
mySearch = function (source : string , subString : string ) {
82
- return source .search (subString ) !== - 1 ;
84
+ return source .search (subString ) !== - 1 ;
83
85
}
84
86
```
85
87
@@ -91,52 +93,52 @@ mySearch = function(source: string, subString: string) {
91
93
92
94
``` ts
93
95
function 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
+ }
99
101
}
100
- let xcatliu = buildName (' Xcat ' , ' Liu ' );
101
- let xcat = buildName (' Xcat ' );
102
+ let tomcat = buildName (' Tom ' , ' Cat ' );
103
+ let tom = buildName (' Tom ' );
102
104
```
103
105
104
106
需要注意的是,可选参数必须接在必需参数后面。换句话说,** 可选参数后面不允许再出现必须参数了** :
105
107
106
108
``` ts
107
109
function 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
+ }
113
115
}
114
- let xcatliu = buildName (' Xcat ' , ' Liu ' );
115
- let xcat = buildName (undefined , ' Xcat ' );
116
+ let tomcat = buildName (' Tom ' , ' Cat ' );
117
+ let tom = buildName (undefined , ' Tom ' );
116
118
117
119
// index.ts(1,40): error TS1016: A required parameter cannot follow an optional parameter.
118
120
```
119
121
120
122
## 参数默认值
121
123
122
- 在 ES6 中,我们允许给函数的参数添加默认值,TypeScript 会将 ** 添加了默认值的参数识别为可选参数 ** :
124
+ 在 ES6 中,我们允许给函数的参数添加默认值,** TypeScript 会将添加了默认值的参数识别为可选参数 ** :
123
125
124
126
``` 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 ;
127
129
}
128
- let xcatliu = buildName (' Xcat ' , ' Liu ' );
129
- let xcat = buildName (' Xcat ' );
130
+ let tomcat = buildName (' Tom ' , ' Cat ' );
131
+ let tom = buildName (' Tom ' );
130
132
```
131
133
132
134
此时就不受「可选参数必须接在必需参数后面」的限制了:
133
135
134
136
``` 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 ;
137
139
}
138
- let xcatliu = buildName (' Xcat ' , ' Liu ' );
139
- let xcat = buildName (undefined , ' Xcat ' );
140
+ let tomcat = buildName (' Tom ' , ' Cat ' );
141
+ let cat = buildName (undefined , ' Cat ' );
140
142
```
141
143
142
144
> 关于默认参数,可以参考 [ ES6 中函数参数的默认值] [ ] 。
@@ -147,9 +149,9 @@ ES6 中,可以使用 `...rest` 的方式获取函数中的剩余参数(rest
147
149
148
150
``` js
149
151
function push (array , ... items ) {
150
- items .forEach (function (item ) {
151
- array .push (item);
152
- });
152
+ items .forEach (function (item ) {
153
+ array .push (item);
154
+ });
153
155
}
154
156
155
157
let a = [];
@@ -160,9 +162,9 @@ push(a, 1, 2, 3);
160
162
161
163
``` ts
162
164
function 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
+ });
166
168
}
167
169
168
170
let a = [];
@@ -181,11 +183,11 @@ push(a, 1, 2, 3);
181
183
182
184
``` ts
183
185
function 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
+ }
189
191
}
190
192
```
191
193
@@ -197,11 +199,11 @@ function reverse(x: number | string): number | string {
197
199
function reverse(x : number ): number ;
198
200
function reverse(x : string ): string ;
199
201
function 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
+ }
205
207
}
206
208
```
207
209
0 commit comments