Thursday, 16 March 2017

Node.js _http_server.js' Server function's first line confusion

I'm reading node.js' http modules' source code. In _http_server.js it has a function

function Server(requestListener) {
  if (!(this instanceof Server)) return new Server(requestListener);
  net.Server.call(this, { allowHalfOpen: true });

  if (requestListener) {
    this.on('request', requestListener);
  }
  ...
}

I can't understand the first line if (!(this instanceof Server)) return new Server(requestListener);.

In other files for example the function http.createServer, this function is called as return new Server(requestListener). Because the object that new create is never an instance of Server function. Won't it be an infinite recursive loop?



via spacegoing

No comments:

Post a Comment