Συναρτήσεις Βέλους στο TypeScript
Στο TypeScript μπορούμε επίσης να δημιουργήσουμε συναρτήσεις βέλους. Ας δούμε ένα παράδειγμα. Ας υποθέσουμε ότι έχουμε την ακόλουθη συνάρτηση:
typescriptlet func = function(num: number): number {
return num ** 2;
}
Ας ξαναγράψουμε αυτή τη συνάρτηση σε μορφή βέλους:
typescriptlet func = (num: number): number => num ** 2;
Задание 1
Μετατρέψτε την ακόλουθη συνάρτηση σε συνάρτηση βέλους:
typescriptlet func = function(num1: number, num2: number): number {
return num1 + num2;
}
Задание 2
Μετατρέψτε την ακόλουθη συνάρτηση σε συνάρτηση βέλους:
typescriptlet func = function(str: string): string[] {
return str.split('');
}
Задание 3
Ξαναγράψτε τον ακόλουθο κώδικα JavaScript σε TypeScript:
javascriptlet arr = [1, 2, 3];
let res = arr.map(num => num ** 2);
console.log(res);