前言
函數(shù)是JavaScript中的一等公民,任何程序都需要使用函數(shù)構(gòu)建抽象,TypeScript為了增強JavaScript中函數(shù)的能力辛块,增加了別的強類型語言函數(shù)的用法。
一抖甘、基本使用
function add(x: number, y: number): number {
return x + y;
}
這里對參數(shù)和函數(shù)的返回值進行類型定義,要求x
和y
必須是numer
類型葫慎,返回值必須是number
類型衔彻。
函數(shù)的返回類型,TypeScript類型檢查系統(tǒng)會根據(jù)函數(shù)體的返回值自行進行判斷偷办,所以你也可以這樣做:
function add(x: number, y: number){
return x + y;
}
這在TypeScript中也是合理的艰额。
也可以使用箭頭函數(shù)定義匿名函數(shù)
let secondAdd: (x: number, y: number) => number;
secondAdd = function(x: number,y: number) {
return x + y;
}
同時和之前的例子一樣,當(dāng)你在函數(shù)引用中定義好了函數(shù)的參數(shù)類型和返回值類型椒涯,那么函數(shù)體不需要再次指定類型:
secondAdd = function(x,y) {
return x + y;
}
二柄沮、可選參數(shù)
在JavaScript中函數(shù)的參數(shù)個數(shù)一旦定義,調(diào)用函數(shù)的時候就必須傳入相同個數(shù)的參數(shù)。
比如:
function xAddy(x, y) {
return x + y;
}
然后調(diào)用的時候我們只傳入一個參數(shù):
xAddy(1)
// Expected 2 arguments, but got 1
TypeScript中我們可以使用可選參數(shù)祖搓,使得函數(shù)的定義更加靈活:
function times(x: number, y?: number) {
if (y) {
return x * y
}
return x;
}
console.log(times(2));
// 2
console.log(times(2,3));
// 6
三狱意、默認(rèn)參數(shù)值
TypeScript中允許給指定參數(shù)值的函數(shù)參數(shù)指定默認(rèn)值:
function secondTtimes(x: number, y = 3) {
return x * y;
}
console.log(times(2));
// 6
四、剩余參數(shù)
JavaScript中ES6也提出了剩余參數(shù)的概念拯欧,TypeScript中只是在此基礎(chǔ)上了加了類型校驗详囤。
function thirdTimes(x: number, ...restOfNum: number[]) {
let result = x;
for (let i = 0; i < restOfNum.length;i++) {
result += restOfNum[i];
}
return result;
}
console.log(thirdTimes(2));
// 2
console.log(thirdTimes(2,3,5));
//10
五、函數(shù)中的this
使用函數(shù)镐作,必定會涉及到this
的指向問題纬纪,我們知道在JavaScript中this
指向是一個讓人很令人頭疼的事情,TypeScript為this
的使用加了新特性:允許你指定this
的指向滑肉,并且可以將指定后的this
作為函數(shù)的參數(shù),this
一旦指定后摘仅,其指向不會再改變靶庙。
定義一個簡單的接口
interface thisObj {
name: string
getName:() => () => string
}
然后這樣來使用這個接口:
let obj: thisObj = {
name: '馬松松',
getName: function(this: thisObj) {
return () => {
return this.name;
}
}
}
let nameFunc = obj.getName();
console.log(nameFunc())
// 馬松松
注意這里我們將this
指定為了thisObj
對象,在之前的JavaScript中娃属,這樣調(diào)用這個函數(shù)六荒,this
的指向是在非嚴(yán)格模式下是全局對象。
不過我這里有個疑問矾端,既然this
可以作為參數(shù)傳遞掏击,那我這樣使用可以嗎?
let obj: thisObj = {
name: '馬松松',
getName: function(this: thisObj) {
return function(this:thisObj) => {
return this.name;
}
}
}
let nameFunc = obj.getName();
console.log(nameFunc())
在這里我沒有使用箭頭函數(shù)秩铆,直接使用匿名函數(shù)砚亭,但是經(jīng)過我的測試,這樣使用竟然是不行的殴玛。
六捅膘、重載
TypeScript中也允許使用重載,所謂的重載是,定義多個函數(shù)名相同滚粟、傳入?yún)?shù)類型不同的函數(shù)寻仗,根據(jù)不同的參數(shù)類型,由編譯器選擇執(zhí)行匹配的函數(shù)凡壤。
首先定義重載列表署尤,TypeScript會對重名的函數(shù)進行類型的匹配:
function overLoadE(x: {name: string,age: number}): string | number;
function overLoadE(x: string | number): {name?: string,age?: number};
function overLoadE(x: any):any{
if (typeof x == 'object') {
return `你的名字是:${x.name},你的年齡是${x.age}`;
}
if (typeof x == "string") {
return {name: x}
}
if (typeof x == 'number') {
return {age: x}
}
}
然后傳入不同的參數(shù):
console.log(overLoadE({name: '馬松松', age: 18}));
// 的名字是:馬松松,你的年齡是18
console.log(overLoadE("馬松松"));
// { name: '馬松松' }
console.log(overLoadE(18));
// { age: 18 }