简体   繁体   English

Node.js _http_server.js的服务器功能的第一行混乱

[英]Node.js _http_server.js' Server function's first line confusion

I'm reading node.js' http modules' source code. 我正在阅读node.js的http模块的源代码。 In _http_server.js it has a function _http_server.js它具有一个功能

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); if (!(this instanceof Server)) return new Server(requestListener);我不明白第一行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) . 在其他文件中,例如函数http.createServer ,此函数称为return new Server(requestListener) Because the object that new create is never an instance of Server function. 因为new创建的对象永远不是Server函数的实例。 Won't it be an infinite recursive loop? 会不会是无限递归循环?


Answers below are great. 下面的答案很好。 But here is a little complement to my confusion: 但这是对我的困惑的补充:

By calling new Foo() , a new object will be created. 通过调用new Foo() ,将创建一个新object Then object 's [[prototype]] will be linked to Foo 's .prototype . 然后object[[prototype]]将链接到Foo.prototype Only after that this will be bound to object and Foo will be executed. 只有在this之后,它将被绑定到object并且Foo将被执行。

Since instanceof check whether Foo 's prototype is in object 's prototype chain, if Server is called by new then the newly created object will be Server 's instance. 因为instanceof检查Fooprototype是否在object的原型链中,所以如果Servernew调用,则新创建的object将是Server的实例。

This is a common idiom to allow instantiation of a class without having to use new . 这是一个常见的习惯用法,允许实例化类而不必使用new In other words, it allows both of these: 换句话说,它允许这两个:

let instance = new Server();
let instance = Server();

That first line is meant to catch the second method of instantiating (which is just a regular function call). 第一行旨在捕获第二种实例化方法(这只是一个常规函数调用)。 In that case, this doesn't refer to the new instance, so this instanceof Server will be false. 在这种情况下, this并不是指新的实例,所以this instanceof Server将是错误的。

When that happens, an instance is creating using new Server() . 发生这种情况时,将使用new Server()创建一个实例。 The same function will be called again, but because of using new the this variable will now point to the newly created instance and the check bypassed (so it doesn't create a recursive call). 将会再次调用相同的函数,但是由于使用new因此this变量现在将指向新创建的实例,并且绕过检查(因此它不会创建递归调用)。

In other files for example the function http.createServer , this function is called as return new Server(requestListener) . 在其他文件中,例如函数http.createServer ,此函数称为return new Server(requestListener) Because the object that new create is never an instance of Server function. 因为new创建的对象永远不是Server函数的实例。

It's an instance of the Server class . 它是Server 的实例。

When you create an element with new , the object created is the instance of the function referenced as new , so when http.createServer calls return new Server(requestListener) then the new element is an instance of Server . 使用new创建元素时,创建的对象是引用为new的函数的实例,因此当http.createServer调用return new Server(requestListener)new元素就是 Server的实例。

This way is impossible to have a loop because when is not an instance, it creates a new instance, being this way: 这种方法不可能产生循环,因为当不是实例时,它将以这种方式创建一个新实例:

  • I'm called, let's check if I'm an instance of me 我被叫了,让我们检查一下我是否是我的一个实例
  • Nope, create a new instance of me 不,创建我的新实例
  • I'm called, let's check if I'm an instance of me 我被叫了,让我们检查一下我是否是我的一个实例
  • Yep, I'm an instance of me. 是的,我是我的一个实例。 Move on 继续

This is done to create a new instance of Server without the need to call new . 这样做是为了创建Server的新实例,而无需调用new

var serverinstance = Server(rqlistener);
var serverinstance2 = new Server(rqlistener);

Both are an instance of Server , because of the inner code of the Server function. 由于Server函数的内部代码,两者都是Server的实例。

Under my opinion, I don't like these kind of hacks btw. 在我看来,我不喜欢这类骇客。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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