Mamy sobie funkcję:
function TYTAN(..){
...
}
Pytanie: Jak odwołać się do poszczególnych parametrów tej funkcji?
Mamy sobie funkcję:
function TYTAN(..){
...
}
Pytanie: Jak odwołać się do poszczególnych parametrów tej funkcji?
Poprawna składnia rest operator składa się z notacji ...
oraz nazwy parametru. W tym wypadku użyłem args
, ale równie dobrze może to być jakaś inna nazwa.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters
function tytan(...args) {
console.log(args); // tablica przekazanych argumentów
}
function tytan2(name, age, ...args) {
console.log(name);
console.log(age);
console.log(args); // tablica pozostałych argumentów
}
Jest też możliwość skorzystania z zmiennej arguments
dostępnej w funkcjach i funkcjach anonimowych (bez arrow-functions):
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
function tytan3() {
console.log(arguments); // tablica argumentów
}
@Xarviel: Nie wprowadzaj w błąd pytających, bo arguments
to wcale nie jest tablica. Owszem, ma .length
, index-access oraz jest iterowalny, ale tablicą nie jest.
Żeby to było tablicą trzeba by zrobić jedno z: Array.from(arguments)
, Array.prototype.slice.call(arguments, 0)
albo [...arguments]
.
@Xarviel: Nie wprowadzaj w błąd pytających, bo arguments to wcale nie jest tablica. Owszem, ma .length, index-access oraz jest iterowalny, ale tablicą nie jest.
Żeby to było tablicą trzeba by zrobić jedno z: Array.from(arguments), Array.prototype.slice.call(arguments, 0) albo [...arguments].
To coś, nazywa się obiekt.