Do not do this.. use modules/packages instead. This type of development is an artifact of older techniques that are not needed today. For that matter every modern browser supports modules (and dynamic import) directly, and there are many, many bundlers if you want to do it ahead of time.
Most of these articles are promoting writing JavaScript like it's still 2010.
https://caniuse.com/#search=modules
It isn't so much about being worthy content... this site serves a very specific topic, and venturing away from that (JavaScript) is frowned upon.
There are lots of articles and tools that get posted on Flutter, Swift, Rust, Go, Commercial Services, etc... and they tend to get removed. The fact that this is more conceptual is why I didn't remove it out of hand.
I almost removed this article, but I'd like to get feedback from the community on this. It isn't JS specifically, but thinking and explaining how to handle certain types of programming problems.
You'd be likely to see questions like this in a FAANG level technical interview.
Would still probably lean into the spread for the nested call... since everything except for IE11 supports it now.
https://caniuse.com/#feat=mdn-javascript_operators_spread_spread_in_function_calls
Same goes for classes actually.
Sigh... the article doesn't even cover what it says in the title.
First, if you use the class syntax, then you will get an error if you don't use new.
class Foo {
// implementation details go here
}
Foo(); // Error: Class constructor Foo cannot be invoked without 'new'
Second, you can do the same kind of behavior in a constructor function.
function Foo() {
if (!(this instanceof Foo)) {
throw new Error("Class constructor Foo cannot be invoked without 'new'");
}
// implementation details go here
}
The article does discuss the issue, but gives no real resolution, such as above.
The article's solution break the type for the returned object... you may as well not use a constructor/class at all if that's your approach, which is fine, but don't use a constructor function naming `Foo` if you're returning a custom object, use `createFoo` or another appropriate name.
Alternatively...
function Foo(...args) {
if (!(this instanceof Foo)) {
return new Foo(...args);
}
...
}
I've effectively stopped supporting IE11 for over 2 years now... Every other browser added async function support by the end of April, 2017, nearly 3 years ago. That's where I cutoff.
IE11 shouldn't be supported at this point imho, and I will not make efforts to do so. Supporting IE11 adds complexity and/or bundle sizes that are excessive to support what aren't even that modern of features at this point.
https://gist.github.com/tracker1/8eea53ec700a0fea725123516a30a70a