I couldn't find this answer on Google because 'on' is such a common word. In this node.js example:
conn.on('close', function() { var pos = connections.indexOf(conn); if (pos >= 0) { connections.splice(pos, 1); } });
There is a .on
method(?). What it does? It is a JavaScript method? Or it is something you only find in node? I'm kind of confused because I think I saw something like .on
on jQuery. Is it similar to the jQuery .live
event handler?
Can anyone explain this to me?
The on() method requires name of the event to handle and callback function which is called when an event is raised. The emit() function raises the specified event. First parameter is name of the event as a string and then arguments. An event can be emitted with zero or more arguments.
Definition and Usage The on() method attaches one or more event handlers for the selected elements and child elements. As of jQuery version 1.7, the on() method is the new replacement for the bind(), live() and delegate() methods.
NodeJS is a cross-platform and opensource Javascript runtime environment that allows the javascript to be run on the server-side. Nodejs allows Javascript code to run outside the browser. Nodejs comes with a lot of modules and mostly used in web development.
The call() method returns the result of calling the functionName() . By default, the this inside the function is set to the global object i.e., window in the web browsers and global in Node. js. Note that in the strict mode, the this inside the function is set to undefined instead of the global object.
It is a method from Node's EventEmitter class:
https://nodejs.org/docs/latest/api/events.html#events_emitter_on_eventname_listener
In this case, on
is a node method. jQuery also has a method of the same name, and they're used for basically the same purpose - binding event handlers to events by their string name. In fact the signatures look identical to me, IIRC.
Pure JavaScript doesn't have such a method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With