3 ms·
I absolutely agree. As a related side rant, the `new` keyword, or equivalent, makes great sense in a manual-memory-management language like C, and it's an offe
by jholman 9y ago
I absolutely agree.
As a related side rant, the `new` keyword, or equivalent, makes great sense in a manual-memory-management language like C, and it's an offense to sanity in a language like Java or Javascript. Factory functions should be the default everywhere. So any JS coding style that encourages `new` is a wildly bad fit to the rest of the language.
But it is important to admit the caveat, as you do, which is "when you create tons of objects".
Such situations are unequivocally in the minority (compared to line-of-business stuff where RAM and CPU more than sufficient to the task), but not the vanishing minority. So we do still need a good solution for those.
Rendering, simulation, sampling/signal-processing, parsing... the list goes on. This isn't truly systems-level code, but it's easy to generate a billion objects per second. So we do need patterns for doing this kind of programming.
- int_19h 9y ago> Factory functions should be the default everywhere. Factory functions still need to create the object somehow to return it.
- jholman 9y agoAbsolutely. The guts of the factory function should usually involve an object literal. Or in some cases, Object.create. (If we're talking about Java, then obviously you have to have `new` somewhere, which is just one of the many shortcomings of Java, although obviously not a fatal one.)
- sethrin 9y agoI do really like JavaScript's ability to compose objects like that, and I feel that people interested in it as a language should definitely learn how to program in that manner. However, I suspect that for most people JavaScript is a means to an end, and it's more useful in a number of senses to try to write JavaScript more like other class-based languages. I mean, I feel like things like TypeScript sort of miss the point of having a dynamically typed language, but there is a solid case to be made for features like strong typing, especially as size and contributors go up.