One, arrow expression
Used to declare anonymous functions, eliminate the This pointer problem for traditional anonymous functions
A single line can be omitted {}, multiple lines cannot be saved.
var sum = (arg1,arg2) = ARG1+ARG2;
Define a lunch function
var dosomething = () =>{
Console.log ("Hahahha");
}
return even
var array = [1,2,3,4]
Console.log (Array.filter (value = = value% 2 = = 0));
, eliminating the this pointer problem for traditional anonymous functions
JavaScript functions
function Getstock (name:string) {
THIS.name = name;
SetInterval (function () {
Console.log ("name is" +this.name);
},2000);
}
var stock =new Getstock ("IBM");
Output Result:
Name is
Switch to Typescript
function Getstock (name:string) {
THIS.name = name;
SetInterval (() =>{
Console.log ("name is" +this.name);
},1000);
}
var stock =new Getstock ("IBM");
Output Result:
Name is IBM
Two, loop foreach (), for in and for
1.forEach () prints only the values in the collection and does not print the group's property values. You can't use break to jump out of this loop.
var myArray = [1, 2, 3];
MYARRAY.DSC = "Hahahhahha";//typescript does not support this notation
Myarray.foreach (value = Console.log (value));
Output Result:
1
2
3
2.for in, the principle is cyclic key-value pairs.
var myArray = [1, 2, 3];
MYARRAY.DSC = "Hahahhahha";//typescript does not support this notation
for (var n in MyArray) {
Console.log (n);
}
Output Result:
0
1
2
Dsc
If you want to print the corresponding value, you can write it
var myArray = [1, 2, 3];
MYARRAY.DSC = "Array description";//typescript does not support this notation
for (var n in MyArray) {
Console.log (Myarray[n]);
}
Output Result:
1
2
3
Array description
The difference between 3.for and foreach is that you can break and jump out of this loop. The loop is a value instead of a key.
var myArray = [1, 2, 3];
for (var n of MyArray) {
Console.log (n);
}
Output Result:
1
2
3