EN
TypeScript - create and use functions
0
points
In TypeScript it is possible to create and use functions in few ways.
1. Function function / class example
const action1 = Function('console.log("This is my message...");');
const action2 = Function('console.log(arguments);');
const action3 = new Function('console.log("This is my message...");');
const action4 = new Function('console.log(arguments);');
action1();
action2(1, 2, 3, 'test 1', 'text 2', 'text n');
action3();
action4(1, 2, 3, 'test 1', 'text 2', 'text n');
console.log(action1.name);
console.log(action2.name);
console.log(action3.name);
console.log(action4.name);
Output (with NodeJS):
This is my message...
[Arguments] {
'0': 1,
'1': 2,
'2': 3,
'3': 'test 1',
'4': 'text 2',
'5': 'text n'
}
This is my message...
[Arguments] {
'0': 1,
'1': 2,
'2': 3,
'3': 'test 1',
'4': 'text 2',
'5': 'text n'
}
anonymous
anonymous
anonymous
anonymous
2. Anonymous function example
2.1. With function name from constiable name
const action = function(): void {
console.log('This is my message...');
};
const sum1 = function(a: number, b: number): number {
return a + b;
};
const sum2 = function(...args: number[]): number {
let result = 0;
for (let i = 0; i < args.length; ++i) {
result += args[i];
}
return result;
};
const display = function(a: number, b: number, ...args: number[]): void {
let text = '';
text += 'a=' + a + ', ';
text += 'b=' + b + ', ';
text += 'args=[';
for (let i = 0; i < args.length; ++i) {
if (i > 0) {
text += ', ';
}
text += args[i];
}
text += ']';
console.log(text);
};
action();
console.log(sum1(1, 2));
console.log(sum2(1, 2, 3, 4, 5));
display(1, 2, 3, 4, 5);
console.log(action.name);
console.log(sum1.name);
console.log(sum2.name);
console.log(display.name);
Output (with NodeJS):
This is my message...
3
15
a=1, b=2, args=[3, 4, 5]
action
sum1
sum2
display
2.2. With own function name
const action = function MyFunctionName(): void {
console.log('This is my message...');
};
const sum1 = function MyFunctionName(a: number, b: number): number {
return a + b;
};
const sum2 = function MyFunctionName(
a: number,
b: number,
...args: number[]
): number {
let result = 0;
for (let i = 0; i < args.length; ++i) {
result += args[i];
}
return result;
};
const display = function MyFunctionName(
a: number,
b: number,
...args: number[]
): void {
let text = '';
text += 'a=' + a + ', ';
text += 'b=' + b + ', ';
text += 'args=[';
for (let i = 0; i < args.length; ++i) {
if (i > 0) {
text += ', ';
}
text += args[i];
}
text += ']';
console.log(text);
};
action();
console.log(sum1(1, 2));
console.log(sum2(1, 2, 3, 4, 5));
display(1, 2, 3, 4, 5);
console.log(action.name);
console.log(sum1.name);
console.log(sum2.name);
console.log(display.name);
Output (with NodeJS):
This is my message...
3
12
a=1, b=2, args=[3, 4, 5]
MyFunctionName
MyFunctionName
MyFunctionName
MyFunctionName
3. Arrow function example
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
const action1 = (): void => {
console.log('This is my message...');
};
const action2 = (...args: number[] | string[]): void => {
console.log(args);
};
const sum = (a: number, b: number): number => {
return a + b;
};
const display = (
a: number | string,
b: number | string,
...args: number[] | string[]
): void => {
let text = '';
text += 'a=' + a + ', ';
text += 'b=' + b + ', ';
text += 'args=[';
for (let i = 0; i < args.length; ++i) {
if (i > 0) {
text += ', ';
}
text += args[i];
}
text += ']';
console.log(text);
};
action1();
action2(1, 2, 3, 4, 5);
console.log(sum(1, 2));
display(1, 2, 3, 4, 5);
console.log(action1.name);
console.log(action2.name);
console.log(sum.name);
console.log(display.name);
Output (with NodeJS):
This is my message...
[ 1, 2, 3, 4, 5 ]
3
a=1, b=2, args=[3, 4, 5]
action1
action2
sum
display
3.2. Without brackets - with auto-returning of values
const action = (): void => console.log('This is my message...');
const print1 = (text: string): void => console.log(text);
const print2 = (...myArguments: string[] | number[]) =>
console.log(myArguments);
const print3 = (
a: string | number,
b: string | number,
...myArguments: string[] | number[]
): void => console.log(a, b, myArguments);
const sum = (a: number, b: number): number => a + b;
action();
print1('This is my custom text...');
print2(1, 2, 3, 4, 5);
print3(1, 2, 3, 4, 5);
console.log(sum(1, 2));
console.log(action.name);
console.log(print1.name);
console.log(print2.name);
console.log(print3.name);
console.log(sum.name);
Output (with NodeJS):
This is my message...
This is my custom text...
[ 1, 2, 3, 4, 5 ]
1 2 [ 3, 4, 5 ]
3
action
print1
print2
print3
sum