#understand bind and prototypage
56 messages · Page 1 of 1 (latest)
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!
what?
none of that seems really connected
can you clarify what you're trying to understand here
the bot.on function expects a parmameter, a string (eventname) and a function/lambda, right?
yeah
so the protoypage of this function as an argument expects a string and a functional interface, right?
no
"prototypage" isn't a term used
prototypes are a completely different thing from this
I mean where it's created (in the djs lib)
what you're describing is the parameter list of the function, which is part of the signature
also no functional interfaces here, that's a java feature
js just has functions, straight up
the form () => {} is an arrow function in js
yes like lambda in other languages i think
so the 2nd args waited is arrow function?
works like a lambda, sometimes but usually not called a lambda, but definitely not a functional interface
any function with the right signature. "arrow function" describes the syntax, not what it creates (although there are some differences between arrow functions and functions made in other ways, but that's not important here)
keep in mind that the types aren't enforced by js
so you could pass any value, or a function with the wrong signature, and js would let you, you'd just get runtime errors
okay and on the event. The djs lib add argument (event) probably in the function first parameter right?
what?
your question isn't coherent there
client.on("messagecreateblabl", (event) => {})
at the message create this function got called and djs give argument for event (with sender etc...) right?
yes
okay so
bot.on(file.split(".js").join(""), event.bind(null, bot))
is a event handler's system
so event.bind(null, bot)) is our function executed on the event. bind is here to pass the client instance (bot) at the module is that?
other argument like (event) or message sometimes are auto passed after bot?
(the module event is like this event(bot, event/message ...)
bind is a method of functions that returns a modified function, that basically pre-sets the this value and however many arguments you give
so if you had a function handleMessage(client, message), calling bind(null, bot) on it would preset the this value as null, the client parameter as bot, and then return a function that's waiting to accept the message still
by just doing event(bot) it would call event at levent time but there couldn't be any other arguments than bot right?
whereas bind allows
It would call the event function when reaching that .on statement, not when the event is emitted in that case. Which you don’t want
ah yes, it wouldn't take into account the "callback"
but why is it that with even an arrow function it waits for the event to activate, whereas if you include a function directly it doesn't?
if you include a function directly it does
but handler(bot) is not a function, it is a function call
why it does for a function but no for arrow function?
it does for an arrow function too
it's passed as a callback, waiting to be called, if you pass a function, any kind, including an arrow function
the difference is that handler(bot) is a function call, not a function.
handler.bind(null, bot) is also a function call. the bind is executed immediately, it just returns another function that's actually passed and used as a callback
ah okay
thanks for your brightening
Is the fact that other args such as event are automatically added after bot a bing-specific property?
function.bind(thisArg, …args) just returns a function that basically is (…otherArgs) => function(…[…args, …otherArgs]) (ignoring how the this parameter gets set)
So is a bind proprety
And bond return
(Otherargs like event) => function(client passed in bind, otherargs like event )?
I‘m sorry but I don’t understand your question
I mean, what is it that makes other argumenhts such as message, (generated by discord api) be added to the suite in our event(bot, "suite") function that is automatically done by bot.on?
The fact that when the bot receives a messageCreate payload from the gateway websocket it‘ll call <Client>.emit('messageCreate', message)
There’s no magic there, it‘s just literally called with those parameters
And other args are auto passed after bot in my event(bot)?
What event(bot)? There is no such function and if you made one named like that you should show it for me to be able to answer that question
event is just a module/function for my event handler (1 file per event) event() wait client instance and event like message
event(bot, message)
but with bind i just do event.bind(null, bot) and i want understand how its possible that message is auto aded after bot for event
because event.bind(null, bot)(message) is the same as event(bot, message) (if we ignore the this reference)
There’s nothing getting Auto added. It‘s literally called with those parameters
so in my bot.on(messagecreate, event.bind(bull, bot)) the bot.on methode is maded to add message in arg at the function in second args of it(bot.on)? right?