在JavaScript里,函数可以使用函数体外部的变量。 当函数这么做时,我们说它‘捕获’了这些变量。
let z = 100; function addToZ(x, y) { return x + y + z; }
一、函数类型
let myAdd: (x: number, y: number) => number = function(x: number, y: number): number { return x + y; };
myAdd 具有 函数类型 (x: number, y: number) => number
匿名函数的参数和返回值也分别具有类型 function(x: number, y: number): number
【注】:推断类型——可以不写左边或者右边的类型,TS会自动推断出来,如:
// myAdd has the full function type let myAdd = function(x: number, y: number): number { return x + y; }; // The parameters `x` and `y` have the type number let myAdd: (baseValue: number, increment: number) => number = function(x, y) { return x + y; };
二、函数参数
——传递给一个函数的参数个数必须与函数期望的参数个数一致。
1.可选参数:
参数可传可不传。 没传参的时候,它的值就是undefined。
function buildName(firstName: string, lastName?: string) { if (lastName) return firstName + " " + lastName; else return firstName; } let result1 = buildName("Bob"); // works correctly now let result2 = buildName("Bob", "Adams", "Sr."); // error, too many parameters let result3 = buildName("Bob", "Adams"); // ah, just right
【注】:可选参数必须跟在必须参数后面
2.默认参数
当用户没有传递这个参数或传递的值是undefined
时,可以为参数提供一个默认值。
function buildName(firstName: string, lastName = "Smith") { return firstName + " " + lastName; } let result1 = buildName("Bob"); // works correctly now, returns "Bob Smith" let result2 = buildName("Bob", undefined); // still works, also returns "Bob Smith" let result3 = buildName("Bob", "Adams", "Sr."); // error, too many parameters let result4 = buildName("Bob", "Adams"); // ah, just right
【注】:默认参数都是可选的,因此可选参数与默认参数 共享参数类型:
(firstName: string, lastName?: string) => string
【注】:带默认值的参数不需要放在必须参数的后面。 如果带默认值的参数出现在必须参数前面,用户必须明确的传入 undefined
值来获得默认值。
3.剩余参数
function buildName(firstName: string, ...restOfName: string[]) { return firstName + " " + restOfName.join(" "); } let buildNameFun: (fname: string, ...rest: string[]) => string = buildName;
三、this
this
的值在函数被调用的时候才会指定。 因此需要知道函数调用的上下文是什么。
1.箭头函数
箭头函数能保存函数创建时的 this
值,而不是调用时的值:
let deck = { suits: ["hearts", "spades", "clubs", "diamonds"], cards: Array(52), createCardPicker: function() { // NOTE: the line below is now an arrow function, allowing us to capture 'this' right here return () => { let pickedCard = Math.floor(Math.random() * 52); let pickedSuit = Math.floor(pickedCard / 13); return {suit: this.suits[pickedSuit], card: pickedCard % 13}; } } } let cardPicker = deck.createCardPicker(); let pickedCard = cardPicker(); alert("card: " + pickedCard.card + " of " + pickedCard.suit);
【疑】this参数、this回调函数里的参数
四、重载
根据传给函数不同的参数,获取不同的处理
let suits = ["hearts", "spades", "clubs", "diamonds"]; function pickCard(x: {suit: string; card: number; }[]): number; function pickCard(x: number): {suit: string; card: number; }; function pickCard(x): any { // 这个不是指参数为any类型的重载,而是前两个重载的实现 // Check to see if we're working with an object/array // if so, they gave us the deck and we'll pick the card if (typeof x == "object") { let pickedCard = Math.floor(Math.random() * x.length); return pickedCard; } // Otherwise just let them pick the card else if (typeof x == "number") { let pickedSuit = Math.floor(x / 13); return { suit: suits[pickedSuit], card: x % 13 }; } } let myDeck = [{ suit: "diamonds", card: 2 }, { suit: "spades", card: 10 }, { suit: "hearts", card: 4 }]; let pickedCard1 = myDeck[pickCard(myDeck)]; alert("card: " + pickedCard1.card + " of " + pickedCard1.suit); let pickedCard2 = pickCard(15); alert("card: " + pickedCard2.card + " of " + pickedCard2.suit);
【注】:function pickCard(x): any
并不是重载列表的一部分,因此这里只有两个重载:一个是接收对象另一个接收数字。 以其它参数调用 pickCard
会产生错误。
来源:https://www.cnblogs.com/shenjie0507/p/10119180.html