#Spread operator in callback function

7 messages · Page 1 of 1 (latest)

grizzled loom

What does the spread operator in a callback function do? I always see on the docs for the Handler client.on(event.name, (client, ...args) and then when calling the function func(client, ...args) but I got some questions. Wouldn't you do func(client, args) after spreading it in the callback parameters and does all the parameters spread when doing ...args? If so, does args hold like an array of all the parameters' values

untold vapor

you're being mislead by the syntax a bit. ... is used for 2 things, rest parameters/destructures and spread syntax
...iterable or ...object is spread syntax
...parameter inside functions or ...elements inside a destructure is a rest parameter/destructure, effectively the inverse of spread
https://mdn.io/rest_parameter
https://mdn.io/destructuring_assignment

grizzled loom

Oh shi right, rest parameters, preciate that

untold vapor

anyways yeah args is an array inside the function
if you had (client, args) at both points that would work as well, but you can't control how djs emits events so you have to conform to djs' pattern of passing the values as separate arguments

terse moat

@saiisnotsxy#5926

untold vapor

they left 💀

bruh