StevenBradley_js•

In case they decide not to use a class in the future. Or more generally, so the library author is free to change the implementation at any time, by keeping the surface area of the public API small and designed for backwards-compatibility.

9
MoTTs_•

Sounds like everyone in this entire thread has just been speculating. This is the commit where winston made that change. They say it was to take advantage of V8 optimizations. The idea in that commit seemed to be to create a new class on the fly for each call to createLogger, and attach the logger levels to that class's prototype rather than to the instance. I'm dubious whether this really achieved an optimization (it sounds like it should do the opposite), but, nonetheless, you wanted to know their reason why, and this is it.

cc u/ViKomprenas

5
ViKomprenasOP•

This doesn't actually reduce the API surface, though, does it? I can still get at the class fairly easily with .constructor. It's straightforward on their part to remove that path (delete X.prototype.constructor), which is why I didn't think it was for forwards compatibility, and still don't think they did a good job -- even of communication -- if that was their intent.

(Nor can I really imagine a scenario where it wouldn't be preferable to use a class for this, or any other example I've seen, although obviously how comfortable to be with that is very subjective.)

-2
StevenBradley_js•

API means the intended functionality. You can usually get through to implementation details via runtime functionality, but that doesn't mean you should rely on that. It's usually not worth the trouble to try to hide these things from the user, but they're still not intended to be used.

5
ViKomprenasOP•

A documented language feature with no mark that it's for internal use (underscores, for instance, a la Python) is unintended functionality? I'm genuinely surprised.

-2
TwiNighty•

Does JavaScript allow you to do it? Yes. Does winston say anything about what winston.createLogger().constructor is? No. Then it is not part of the winston API.

You should not rely on any behavior of winston.createLogger().constructor in the same sense that you should not rely on winston.createAwesomeLogger being undefined, or (if you want to get extreme) winston.createLogger().constructor.toString()[6] being 'D'

3
archivedsofa•

Some libraries such as React use ES6 classes, but generally speaking classes are not that common in the JavaScript world. In fact a large part of the community opposes classes, and the ES6 implementation is still heavily criticized and discussed, even though it's really only syntactic sugar over prototype.

If you subscribe to the ES Discuss mailing list, where ES proposals are discussed and championed to the TC39 members, even today people are bitter about the inclusion of classes into EcmaScript.

That said, what u/StevenBradley_js pointed out about keep the options open is also true and may very well be the reason this particular library uses the factory pattern.

4
[deleted]•

[deleted]

-1
MaxPower15•

One reason to not expose a class is that repeated object instantiation in JavaScript can be expensive, and if you think you might use resource pooling in the future, requiring the end user to do new Thing() defeats that; it will always allocate an object.

Another possibility is that you might not want the end user taking your class and extending it—maybe you think of it as an implementation detail. So while you can’t 100% get around that in JavaScript today, not providing the class itself subtly communicates that it’s not meant to be used that way and you’re on your own if things go badly.

0
aesamattki•

There are no classes in JavaScript. ES6 classes are just prototypes wearing a mask, and people who use or want them don't understand the prototypal inheritance model of JavaScript. They should never have been added.

-5
Reashu•

"Addition is just repeated incrementation, and people who want to use it don't understand the basic sequence of numbers."

2
aesamattki•

That's not a good comparison. If you go in to this thinking JavaScript has classes then you'll eventually not be able to debug your code because they don't work the same way as actually classes.

-2
ViKomprenasOP•

As archivedsofa was saying...

-2