If you are familiar with C# params keyword, The rest parameter is the same. The rest parameter lets you specify a function parameter that takes an argument where the number of arguments is variable.
An example could be a function that make a Sum of a variable length of numbers:
Sum(1,2,3,4);
C# example
public int Sum(params int[] list)
{
int sum = 0;
foreach (int i in list)
sum += i;
return sum;
}
TypeScript examplepublic Sum(...list Number[]) : number
{
return list.reduce(
(a,b) => { a + b }
);
}
And the result when compiled to JavaScriptfunction sum() {
var numbers = [];
for (var _i = 0; _i < (arguments.length - 0); _i++) {
numbers[_i] = arguments[_i + 0];
}
return numbers.reduce(function (a, b) {
return a + b;
});
}
No comments:
Post a Comment