[英]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
检查Foo
的prototype
是否在object
的原型链中,所以如果Server
被new
调用,则新创建的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 asreturn new Server(requestListener)
.在其他文件中,例如函数
http.createServer
,此函数称为return new Server(requestListener)
。 Because the object thatnew
create is never an instance ofServer
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: 这种方法不可能产生循环,因为当不是实例时,它将以这种方式创建一个新实例:
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.