Don't Use the Class Keyword in JavaScript

preview_player
Показать описание
I wanted to do a quick follow up video on why you should not use the Class keyword in JavaScript. I have a couple of blog posts that I have make on object creation in JavaScript. I wanted to use this video to show some examples of using the 'class' keyword, and why I feel that object factories in JavaScript are better for object creation.

Рекомендации по теме
Комментарии
Автор

Doesn't really have anything to do with the class keyword. You can encapsulate a class in a factory function as well keeping it sealed in its scope. The benefit would be not redefining a function for every object instance you create. only one prototype object with all the function defenitions and the instance as a context, in stead of many object redefining the same functions over and over.

TroenderTass
Автор

I am 100% on board with this, but unfortunately, I'm currently stuck on an Angular project at the moment. I feel like I'm being railroaded into trying to pretend I'm using Java, for reasons that I suspect are highly dubious.

Angular style guide says don't have more than one thing in a file. Well...guess what... A non-exported local variable is functionally equivalent to a private property. Thanks to closure, it might actually be more private than a TS private property. (I'd have to check, but I think it's only "private" at compile-time). And I personally think the "this" keyword is ugly and just clutters up the code. In fact, a JS module is pretty much functionally equivalent to a singleton class, just by the nature of how modules work. If you want instances, that's not hard to do either. Everything is an object, any function can return an object, etc...

Just learn how JS actually works instead of trying to ape Java for Christ's sake. (A) Java is terrible in the first place, and (B) JS only gives you a fake version of it; it's just syntax sugar for prototype delegation, which is fundamentally different from how classes work in class-based languages.

Out of rebelliousness, I refactored a service class (which was just a bag a helper functions basically...not how services are meant to be used even according to the Angular docs) into a util file with a bunch of functions composed in point-free style that is functionally the same and has a third as many lines of code.

Maybe it's just something about how my brain works... in high school, I breezed through AP physics a year early. Got an A, never studied... It was just plugging stuff into formulas. It seemed so obvious and logical to me. Class-based OOP OTOH feels like constantly trying to jam square pegs into round holes, and erecting all these convoluted structures that serve no purpose beyond adhering to an arbitrary set of rules and conventions.

algp
Автор

Very interesting point.... makes you think

michaelgidron
Автор

Forgive me if I'm missing the point, but it seems to me that favoring composition over inheritance is a different argument than "avoid classes, " although I absolutely understand how they're related. That said, classes *do* provide several other useful features besides just enabling inheritance, no?

peterbailey
Автор

Huuumm, and about # key to set a var to private in a class?

marcelomagalhaes
Автор

Very interesting. Aren't classes pretty much functions under the hood? Thanks.

eleah
Автор

Do those kitties know that masks don't work?

coolworx