First of all: Do not confuse new require("...")
(which invokes require
as a constructor) with new (require("..."))
(which invokes the return value of the require
call as a constructor). You are doing the first one.
require
was not intended to be invoked as a constructor with new
. That's not Node-idiomatic, and generally weird. You shouldn't do it, since it reduces the readbility of your code. A future reader might easily mistake it for new (require("..."))
(i.e., calling new
on a constructor that is being returned by require
), since normal Node style does not use new
with require
.
Now, let's talk about actual side effects.
new
causes a function to run its [[Construct]]
internal method, which takes the following actions:
The return value new require
will be the same as require
for all modules whose exports
value is non-primitive (which is true of virtually any module; they typically export a plain object or a function, which is also a kind of object). In the rare case that your module does export a primitive, though, then new
will deny you access to that value.
The only other possible difference between require(...)
and new require(...)
is that the new
variant is supplied with a different this
value. However, require
appears to ignore its this
value totally. (Note that Module.prototype.require
-- a different function from normal require
-- does use its this
value, but that's only used when you require submodules from a module, using module.require(...)
.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…