EN
TypeScript - create and use functions
0 points
In TypeScript it is possible to create and use functions in few ways.
xxxxxxxxxx
1
const action1 = Function('console.log("This is my message...");');
2
const action2 = Function('console.log(arguments);');
3
const action3 = new Function('console.log("This is my message...");');
4
const action4 = new Function('console.log(arguments);');
5
6
action1();
7
action2(1, 2, 3, 'test 1', 'text 2', 'text n');
8
action3();
9
action4(1, 2, 3, 'test 1', 'text 2', 'text n');
10
11
console.log(action1.name);
12
console.log(action2.name);
13
console.log(action3.name);
14
console.log(action4.name);
Output (with NodeJS):
xxxxxxxxxx
1
This is my message...
2
3
[Arguments] {
4
'0': 1,
5
'1': 2,
6
'2': 3,
7
'3': 'test 1',
8
'4': 'text 2',
9
'5': 'text n'
10
}
11
12
This is my message...
13
14
[Arguments] {
15
'0': 1,
16
'1': 2,
17
'2': 3,
18
'3': 'test 1',
19
'4': 'text 2',
20
'5': 'text n'
21
}
22
23
anonymous
24
anonymous
25
anonymous
26
anonymous
xxxxxxxxxx
1
const action = function(): void {
2
console.log('This is my message...');
3
};
4
5
const sum1 = function(a: number, b: number): number {
6
return a + b;
7
};
8
9
const sum2 = function(args: number[]): number {
10
let result = 0;
11
for (let i = 0; i < args.length; ++i) {
12
result += args[i];
13
}
14
return result;
15
};
16
17
const display = function(a: number, b: number, args: number[]): void {
18
let text = '';
19
text += 'a=' + a + ', ';
20
text += 'b=' + b + ', ';
21
text += 'args=[';
22
for (let i = 0; i < args.length; ++i) {
23
if (i > 0) {
24
text += ', ';
25
}
26
text += args[i];
27
}
28
text += ']';
29
console.log(text);
30
};
31
32
action();
33
console.log(sum1(1, 2));
34
console.log(sum2(1, 2, 3, 4, 5));
35
display(1, 2, 3, 4, 5);
36
37
console.log(action.name);
38
console.log(sum1.name);
39
console.log(sum2.name);
40
console.log(display.name);
Output (with NodeJS):
xxxxxxxxxx
1
This is my message...
2
3
3
15
4
a=1, b=2, args=[3, 4, 5]
5
action
6
sum1
7
sum2
8
display
xxxxxxxxxx
1
const action = function MyFunctionName(): void {
2
console.log('This is my message...');
3
};
4
5
const sum1 = function MyFunctionName(a: number, b: number): number {
6
return a + b;
7
};
8
9
const sum2 = function MyFunctionName(
10
a: number,
11
b: number,
12
args: number[]
13
): number {
14
let result = 0;
15
16
for (let i = 0; i < args.length; ++i) {
17
result += args[i];
18
}
19
20
return result;
21
};
22
23
const display = function MyFunctionName(
24
a: number,
25
b: number,
26
args: number[]
27
): void {
28
let text = '';
29
30
text += 'a=' + a + ', ';
31
text += 'b=' + b + ', ';
32
text += 'args=[';
33
34
for (let i = 0; i < args.length; ++i) {
35
if (i > 0) {
36
text += ', ';
37
}
38
text += args[i];
39
}
40
41
text += ']';
42
43
console.log(text);
44
};
45
46
action();
47
console.log(sum1(1, 2));
48
console.log(sum2(1, 2, 3, 4, 5));
49
display(1, 2, 3, 4, 5);
50
51
console.log(action.name);
52
console.log(sum1.name);
53
console.log(sum2.name);
54
console.log(display.name);
Output (with NodeJS):
xxxxxxxxxx
1
This is my message...
2
3
3
12
4
a=1, b=2, args=[3, 4, 5]
5
MyFunctionName
6
MyFunctionName
7
MyFunctionName
8
MyFunctionName
Arrow function has been introduced in ES6. This part shows in simple way how to use arrow functions.
3.1. With function body in brackets
xxxxxxxxxx
1
const action1 = (): void => {
2
console.log('This is my message...');
3
};
4
5
const action2 = (args: number[] | string[]): void => {
6
console.log(args);
7
};
8
9
const sum = (a: number, b: number): number => {
10
return a + b;
11
};
12
13
const display = (
14
a: number | string,
15
b: number | string,
16
args: number[] | string[]
17
): void => {
18
let text = '';
19
20
text += 'a=' + a + ', ';
21
text += 'b=' + b + ', ';
22
text += 'args=[';
23
24
for (let i = 0; i < args.length; ++i) {
25
if (i > 0) {
26
text += ', ';
27
}
28
text += args[i];
29
}
30
31
text += ']';
32
33
console.log(text);
34
};
35
36
action1();
37
action2(1, 2, 3, 4, 5);
38
console.log(sum(1, 2));
39
display(1, 2, 3, 4, 5);
40
41
console.log(action1.name);
42
console.log(action2.name);
43
console.log(sum.name);
44
console.log(display.name);
45
Output (with NodeJS):
xxxxxxxxxx
1
This is my message...
2
[ 1, 2, 3, 4, 5 ]
3
3
4
a=1, b=2, args=[3, 4, 5]
5
6
action1
7
action2
8
sum
9
display
3.2. Without brackets - with auto-returning of values
xxxxxxxxxx
1
const action = (): void => console.log('This is my message...');
2
3
const print1 = (text: string): void => console.log(text);
4
const print2 = (myArguments: string[] | number[]) =>
5
console.log(myArguments);
6
const print3 = (
7
a: string | number,
8
b: string | number,
9
myArguments: string[] | number[]
10
): void => console.log(a, b, myArguments);
11
12
const sum = (a: number, b: number): number => a + b;
13
14
action();
15
print1('This is my custom text...');
16
print2(1, 2, 3, 4, 5);
17
print3(1, 2, 3, 4, 5);
18
console.log(sum(1, 2));
19
20
console.log(action.name);
21
console.log(print1.name);
22
console.log(print2.name);
23
console.log(print3.name);
24
console.log(sum.name);
Output (with NodeJS):
xxxxxxxxxx
1
This is my message...
2
This is my custom text...
3
[ 1, 2, 3, 4, 5 ]
4
1 2 [ 3, 4, 5 ]
5
3
6
7
action
8
print1
9
print2
10
print3
11
sum