Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript call a function on event

Please explain me the difference between these two statements. which one is calling the function 'connect'. connect is a user defined function.

`peer.on('connection', connect);`

and

f.on('open', function() {
    connect(f);
    });
like image 311
Sadiq Ahmad Avatar asked Jul 22 '26 17:07

Sadiq Ahmad


1 Answers

Both will call connect when the event occurs. The main difference is that the first one gets its arguments set by the event subsystem since it is called directly by whatever manages the events. If your function either doesn't use any arguments or its arguments match up exactly with what the event system passes, then the first one works fine.

In the second one, you control the arguments sent to connect(f) so you can make the arguments anything you want. So, if you want to control the arguments yourself, then the second block of code gives you that option.

like image 118
jfriend00 Avatar answered Jul 24 '26 07:07

jfriend00



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!