December 2017
Beginner
372 pages
10h 32m
English
Generic functions are very similar to normal functions, except for how we specify the types used in a function. Let's take a standard function, and see how it can be converted to a generic function.
The following function takes a number as the input parameter and pushes that number into an array:
data = []; pushNumberToArray(item: number){ this.data.push(item); }
Now, let's say that we get another requirement in the future of pushing a string into the same array. To handle this requirement, we will write another function, which takes a string as the input, as follows:
pushStringToArray(item: string){ this.data.push(item); }
As you can see, this is not the best practice to manage the array. Every time we have a new requirement, ...
Read now
Unlock full access