简体   繁体   中英

Where does this argument (c) come from? - net.createServer(function(c){…}

http://nodejs.org/api/net.html

I read the documentation for the net module, but cannot understand where the callback argument comes from in this example.

var server = net.createServer(function(c) { //'connection' listener
  console.log('server connected');
  c.on('end', function() {
    console.log('server disconnected');
  });
  c.write('hello\r\n');
  c.pipe(c);
});

I don't understand what the passed in 'c' is or where it comes from when the client makes a connection to the server. I am a beginner to Javascript so maybe I am missing something.

createServer is a function that accepts a callback. That is the function(...) {...} construct you see there.

The c argument is passed to the callback by createServer .

If it helps to compare to something better-known, consider this:

someElement.addEventListener("click",function(evt) {
    // here, evt is the event object passed to the callback
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM