#understand bind and prototypage

56 messages · Page 1 of 1 (latest)

stray moon

hello for example a discordjs event is like this:
bot.on(event_string, lamda function)
so the prototypage of bot.on is a string and interface? bot.on(event_name, interface_x)?

arctic starBOT
  • 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!
cinder plume

what?

none of that seems really connected

can you clarify what you're trying to understand here

stray moon
cinder plume

yeah

stray moon
cinder plume yeah

so the protoypage of this function as an argument expects a string and a functional interface, right?

cinder plume

no

"prototypage" isn't a term used

prototypes are a completely different thing from this

stray moon

I mean where it's created (in the djs lib)

cinder plume

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

stray moon

yes like lambda in other languages i think

so the 2nd args waited is arrow function?

cinder plume

works like a lambda, sometimes but usually not called a lambda, but definitely not a functional interface

cinder plume
stray moon so the 2nd args waited is arrow function?

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

stray moon
cinder plume

what?

your question isn't coherent there

stray moon

client.on("messagecreateblabl", (event) => {})

at the message create this function got called and djs give argument for event (with sender etc...) right?

cinder plume

yes

stray moon

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 ...)

cinder plume

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

stray moon

whereas bind allows

serene spire

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

stray moon

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?

cinder plume

if you include a function directly it does

but handler(bot) is not a function, it is a function call

stray moon
cinder plume

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

stray moon

ah okay

thanks for your brightening

stray moon
serene spire

function.bind(thisArg, …args) just returns a function that basically is (…otherArgs) => function(…[…args, …otherArgs]) (ignoring how the this parameter gets set)

stray moon
serene spire

I‘m sorry but I don’t understand your question

stray moon
serene spire

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

stray moon
serene spire

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

stray moon
serene spire

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

stray moon

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?