Here is the doc I am confused with.
When using pre-ES6 style constructors
const { Transform } = require('stream');
const util = require('util');
function MyTransform(options) {
if (!(this instanceof MyTransform))
return new MyTransform(options);
Transform.call(this, options);
}
util.inherits(MyTransform, Transform);
Why do we need to check
this instanceof MyTransform
? As far as I know, as long as we invokenew MyTransform()
, evaluation ofthis instanceof MyTransfrom
will always returntrue
. Maybe usingMyTransform()
to create aTransform
instance can be found in many code bases? This is the only reason I could guess.What is the purpose of
util.inherits(MyTransform, Transform);
? Just to ensure thatnew MyTransform() instanceof Transform
returnstrue
?
Thank you for your time in advance!