Amusing bug: console.log(`Average height for dogs is ${AVERAGE_HEIGHT_FT} kg`); Height in kg? ;D
Those are feet kgs. Like light years.
How to rewrite classes using closures in JavaScript
31–40 of 58 posts
Re: How to rewrite classes using closures in JavaScript
#32There are several points in the article which are just plain wrong: - no private properties ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... ) - no readonly properties: You can use a getter. Or define the property as non-writable, the syntax isn't nice but in my opinion it's still much nicer than what the article proposes. Some of the points are just a matter of taste ("this" is awkward) There's the…
Re: How to rewrite classes using closures in JavaScript
#33I wonder if React has had any impact on people's general tendency for reaching for classes vs. closures, particularly after hooks were introduced.
The machinery behind the scenes might be ugly, but this syntax is also what I love about React Hooks.
I was preferring closures to ES6 classes before using React, despite the performance implications.
Since switching to TypeScript I also like ES classes though.
Especially for things like MobX... but even without it.
Regardless of React, I feel like classes are the context where getters and setters feel most natural and ergonomic.
And classes are great for grouping state and behavior in an easy-to-understand way.
Re: How to rewrite classes using closures in JavaScript
#34I wonder if React has had any impact on people's general tendency for reaching for classes vs. closures, particularly after hooks were introduced.
Re: How to rewrite classes using closures in JavaScript
#35Other commenters have pointed out some of the technical issues with this implementation (all instances get a copy of every method, no prototype-based type checking, non-standard use of "init" vs new, etc.) But the much more important issue is that you did not make life easier for other developers looking at your code. Instead, you just forced them to learn one additional, non-standard way of doing things with very little, if any, real benefit.
Developers need to learn to be extremely judicious whenever you use unusual/non-standard patterns in code. Everyone already needs to learn the standard way of doing things, so there should be a giant bar in terms of things like productivity enhancements or performance improvements if you're forcing everyone to learn your non-standard pattern. Avoiding "this" doesn't cut it.
Re: How to rewrite classes using closures in JavaScript
#36There are several points in the article which are just plain wrong: - no private properties ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... ) - no readonly properties: You can use a getter. Or define the property as non-writable, the syntax isn't nice but in my opinion it's still much nicer than what the article proposes. Some of the points are just a matter of taste ("this" is awkward) There's the…
Agreed. I spent a small epoch of my career trying to make js have "classes" in the heady days of OOP. It took a bit to realize that idiomatic js didn't need them. Besides being sold so hard on OOP as a concept, failing to understand prototypes was one of my biggest mistakes. By the time they landed, I was actually pretty disappointed to see the language get classes.
TBF classes are just a layer of syntactic sugar over the (awful) prototypal system (which somewhat sadly few were interested in making better let alone good).
Behind the scenes, they pretty much just create a ctor function and prototype function, except you don't have to mess with the terrible `Object.create` and `.prototype =` nonsense.
Abandoning constructors and going full-on with prototypes would be an other thing entirely, but the reality is that javascript is not very good at it so it's not very fun (except as a proof of concept / investigation of prototype based OO), and pretty much nobody is interested.
Re: How to rewrite classes using closures in JavaScript
#37Re: How to rewrite classes using closures in JavaScript
#38Here's the example code: https://gist.github.com/conartist6/95f5be3488b2ae6c0dfd9a886...
Re: How to rewrite classes using closures in JavaScript
#39There are private properties in classes class Foo { #ImPrivate = true; #meToo() { if(this.#ImPrivate) cantTouchThis(); } } new Foo().#meToo() === BOOM Also with using closures for "classes" takes up a whole lot more memory because new instances off all the closures are created for each instance of the class.
Private properties don't work with Proxy. https://lea.verou.me/blog/2023/04/private-fields-considered-... Quote: > Private fields, proxies, pick one, you can’t have both.
And thank god for TypeScript compiler's private keyword! While... I can see how this plethora of possibilities looks confusing.
Re: How to rewrite classes using closures in JavaScript
#40I can't tell from a glance from either before or after what the fields of the class are, let alone what sort of data goes in them. I suspect my IDE would have a struggle as well, and allow mistakes, at runtime, to either raise exceptions, or perform incorrect behavior.
I think maybe a cleaner approach in this domain may be TS interfaces + standalone functions that act on them.