Typescript Introductory Knowledge III (expressions and loops)

Source: Internet
Author: User
Tags foreach anonymous setinterval

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

  

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.