In JavaScript, there are two ways of defining a method.
1, the method of naming
function Add (x, y ) {return x+y;}
2. Anonymous method
var function return x+y;};
In TypeScript, also compatible with the above two definitions, but, since we are using TypeScript, then certainly stronger than the original definition of the way.
1, the type of method
function Add (X:number, Y:number): number{ return x+y;} var function (X:number, Y:number): number {return x+y;};
Defining a good type for parameters and return values is useful for IntelliSense and program logic.
2. Function type variable
In the 1th above, the type of the Myadd variable we did not specify, the complete wording is as follows:
var function return x+y; };
Of course, the naming of type parameters can be different
var function (X:number, Y:number): number {return x+y;};
3. Optional parameters
function buildname (firstname:string, LastName?: String): string{ if(lastName) { return FirstName + "" + LastName; } Else { return firstName; }}
After the parameter name, a question mark is added before the colon, which indicates that the parameter is optional.
4. Default parameters
function buildname (firstname:string, lastName = "Smith"): string { return firstName + "" + LastName;}
If you do not pass in the second argument, it will be assigned the default value.
Note: Optional parameters and default parameters must be at the end of the parameter list.
5. Variable parameters
function buildname (firstname:string, ... restofname:string[]) { return firstName + "" + Restofname.join (" ");} var employeename = Buildname ("Joseph", "Samuel", "Lucas", "Mackinzie");
Using three points in front of the parameter name indicates that the number of the parameter is variable, and also must be at the end of the parameter list.
6. Lambda and current object
In JavaScript, the This keyword refers to the current function. Then there is a good chance that we will use the wrong way to define the method. See below:
varDeck ={suits: ["Hearts", "spades", "clubs", "Diamonds"], Cards:array (52), Createcardpicker:function() { return function() { varPickedcard = Math.floor (Math.random () * 52); varPickedsuit = Math.floor (PICKEDCARD/13); return{suit: This. suits[pickedsuit], Card:pickedcard% 13}; } }}varCardpicker =Deck.createcardpicker ();varPickedcard =Cardpicker (); Alert ("Card:" + Pickedcard.card + "of" + pickedcard.suit);
Then, when invoking the Createcardpicker method, we will not get the expected result, because this refers to the current function, not the deck object.
To address this, TypeScript introduces lambda notation
varDeck ={suits: ["Hearts", "spades", "clubs", "Diamonds"], Cards:array (52), Createcardpicker:function() { //notice:the line Below are now a lambda, allowing us to capture ' this ' earlier return() = { varPickedcard = Math.floor (Math.random () * 52); varPickedsuit = Math.floor (PICKEDCARD/13); return{suit: This. suits[pickedsuit], Card:pickedcard% 13}; } }}varCardpicker =Deck.createcardpicker ();varPickedcard =Cardpicker (); Alert ("Card:" + Pickedcard.card + "of" + pickedcard.suit);
So now this, refers to the deck object.
7. Method overloading
Because of the syntax of JavaScript, there is no overload. As long as you have defined a method in JavaScript, it is possible to pass in as many parameters as you want. However, from the example above TypeScript, we can see that TypeScript will check the number of parameters and whether the type matches. Therefore, there is a need for overloading in TypeScript.
varsuits = ["Hearts", "spades", "clubs", "Diamonds"];functionPickcard (x: {suit:string; card:number;} []): number;functionPickcard (X:number): {suit:string; card:number;};functionPickcard (x): 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(typeofx = = "Object") { varPickedcard = Math.floor (Math.random () *x.length); returnPickedcard; } //Otherwise Just let them pick the card Else if(typeofx = = "Number") { varPickedsuit = Math.floor (X/13); return{suit:suits[pickedsuit], card:x% 13 }; }}varMydeck = [{suit: "Diamonds", card:2}, {suit: "Spades", card:10}, {suit: "Hearts", Card:4 }];varPickedCard1 =Mydeck[pickcard (Mydeck)];alert ("Card:" + Pickedcard1.card + "of" +pickedcard1.suit);varPickedCard2 = Pickcard (15); alert ("Card:" + Pickedcard2.card + "of" + pickedcard2.suit);
The above code is the official example of TypeScript, in fact, we can see that the method body needs to be written in a parameter type, the return type of any method. So it seems to be a little self-deception. The significance of method overloading in TypeScript is mainly intelligent hints.
TypeScript TypeScript Learn the 5--method