So I have done some reading on the new.target
boolean added in Node 6.x. Here is a simple example of new.target
provided on MDN
function Foo() {
if (!new.target) throw "Foo() must be called with new";
console.log("Foo instantiated with new");
}
Foo(); // throws "Foo() must be called with new"
new Foo(); // logs "Foo instantiated with new"
But this reads a lot like what I am presently using the below code for
var Foo = function (options) {
if (!(this instanceof Foo)) {
return new Foo(options);
}
// do stuff here
}
My question is this: Is there any benifit to new.target
over the instance of method? I don't particularly see either as more clear. new.target
may be a scosche easier to read but only because it has one less set of parens ()
.
Can anyone provide an insight I am missing? Thanks!