Is the 'on' method in this node.js code a JavaScript method or a node method?


Question

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?

1
37
11/18/2011 6:47:08 PM

Accepted Answer

31
8/11/2016 7:15:24 PM

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.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon