9 ms·
How JavaScript works: 3 types of polymorphism
- rahulsingh789 5y agonice
- elchief 5y agoI'd say inheritance is NOT a good use-case for Person / Employee A person can play many roles, at the same time. Composition is better in this case. A person has many roles Inheritance is better for types of legal parties though (see The Party Model). A person is a legal party. An company is an organisation is a legal party
- wmichelin 5y agoLove to see this line of thinking taking over in the industry. Languages like Go make this type of object relationship super easy.
- nine_k 5y agoI'd say that inheritance, with method overriding, is the worst anti-pattern of mainstream OOP, which contains a number of otherwise sound ideas, like encapsulation. I like the Go's approach to that (taken from Oberon), and the Rust's system of traits. In one large codebase I saw there was a rule to make classes either abstract (without implementation of key parts), or final. Everything else is done via interfaces and composition. See https://en.m.wikipedia.org/wiki/Composition_over_inheritance https://en.m.wikipedia.org/wiki/Composition_over_inheritance
- edoceo 5y agoHooray Traits! Love them, they are even in PHP so I can use them in these older apps I maintain.
- deleted 5y ago[deleted]
- pjmlp 5y agoOberon is fully inheritance based, with interfaces only later introduced in Oberon.NET (for compatibility purposes with .NET) and Active Oberon. Go only took the method syntax from Oberon-2, package initialization and unsafe instead of SYSTEM.
- plexicle 5y agoI personally think composition is better in almost every imaginable case. But I suppose that's an argument for another thread.
- deleted 5y ago[deleted]
- neilv 5y ago> I'd say inheritance is NOT a good use-case for Person / Employee Good call. If I wanted Person distinct from Employee, how I'd probably start, in an analysis model, is have Employment as an association/relationship between Person and Company. In my meta-model, associations like Employment can have attributes, and that might be where things like "employee number" and "salary" come in. And, taking that a little further, we can represent the history of how things like salary change over time. (Meanwhile, a project that went with only an Employee record/object might not have saved any coding time initially, and they're quickly piling on expensive, hard-to-maintain kludges, just to meet evolving business needs. Even the first time an employee got a raise, they might've already been hitting incorrectness cases, just trying to get two parts of the system to use the right salary for the right time.)
- jollybean 5y agoThe very title is kinda scary, in the context that everything was kind of hacked onto JS. While it's probably important to understand, the 'answer' I think, without being ham-fisted, is probably to use Typescript in most cases.
- vgvvgvhvh 5y agoThis isn’t the sort of website where you can criticize tools and languages like a hacker might. This is a forum for people with hire people who will inevitably be forced to make products using these tools for this tower of babel-esque dumpster heap we call the web stack. Just because this forum is called hacker news don’t get your expectations twisted. We’re only here to boost engagement for acquisitions and “growth hacking”
- jrsala 5y agoThere is a lot of wrong in this article: inability to articulate what polymorphism is, misunderstanding of prototypes, bad JS practices (`var`, `.__proto__`), falsehoods on programming languages including JS itself, confusion around ECS, unfinished examples... I suspect the author is in the early stages of their learning journey and Dunning-Kruger is in full effect.
- plexicle 5y agoYou might be right but your last sentence is the reason why people are apprehensive about sharing their work and writing more articles. There's no need for it.
- jrsala 5y agoThere is no judgment in what I said. DK is a fact of life and we will all experience it at some point. I know I have, and quite badly. I wish when I was younger someone had told me "look, you don't really know what you're talking about, here's how things really work". If no one tells you, how long does it take for you to realize and how much time do you waste thinking you know what you don't know? Again, the author is at the start of their learning journey. I'm not saying they're a bad person or a hopeless case. And finally, people should not be apprehensive about sharing their work in general, but I think they should be when writing articles that purport to teach others.
- graderjs 5y agoOkay, "You don't know what you're talking about." :) There is a clear judgement on your part. For me my own recent DK-moment was I'm learning a foreign language and in a shop asked for a simple item with a simple phrase, sure I had it right. They had no idea what I meant and I got angry because I thought you were just trolling me. Relating situation to a friend who let me know my accent was just wrong, so hard to understand. What got me is sometimes my accent good, sometimes it's not, day by day. But I was so sure, because my level is quite low. Haha For you, with your judgement, you don't know whether it's true or not they are in DK, additionally you yourself may be mistaken in your JS criticism, it could be you that has DK, and they are correct. Also, "You don't know what you're talking about.", OK, so, " this is how it works": generally when you criticize someone as doing X, and then backpaddle on pushback to say you do the same, you should recount a specific instance where you did X "and quite badly" because that shows you're genuine, and willing to be vulnerable, as you have tried to make the other person vulnerable by invoking the criticism you, by that omission, seem reluctant to invoke upon yourself. :) ;p xx For me, a recent one where I didn't do that was a friend was telling me about their job offer progress, and salary negotiation. They wanted Y, but didn't say, and company was offering less. I told them how they should just ask for what they want, and try to get it. Friend pushed back, and got upset. Fast forward to same friend, next job offer, this time I shared a story how I hadn't pushed for my preferred salary in a previous job, then told how I felt not enjoying the job and feeling I was not getting fair reward each work day. Much better conversational outcome, friend more receptive to advice this time. Friend ended up with high salary they were happy with. Better yet lead with the story of your mistake, tho that's hard to do. Either way, sharing your own makes you seem more credible in trying to bring someone up not put them down, shows you speak from experience, and engages someone emotionally into feeling for you rather than just being defensive :) ;p xx
- FractalHQ 5y agoAs someone new to JS, a friend of mine insists that I always use classes and inheritance, and that functional style / composition is bad. However, most of the newer libraries I read through all seem pretty functional, whereas the older ones are full of classes. Personally, I find myself writing everything with factory functions and composition because it feels natural to the way I think about code. Am I wrong, and should I be listening to my friend? He has much more experience than me.
- langager 5y agoI've been getting paid to write JS/TS for about 10 years. I think I've used the `class` keyword less than 5 times in the last 5 years and feel better off without it. I've found that a more functional style leads to better separation of state/data from any logic you have to write, allowing for a better testing experience and an easier time refactoring. Plus it means I have to think way less about what `this` means. At the end of the day, features of languages are just tools, and sometimes they fit the problem you're trying to solve. I think it's also important to decide what tools to use in the context of what experience you and your team already have. Also, it's hard to have small .js files when you use classes, and I love small files. I think Go made a great design decision to allow functions on structs to be implemented in separate files.
- cout 5y agoI once heard someone tell me that good OOP ends up looking very functional. I've taken that to heart, and I believe the code I write is better as a result. I do use the `class` keyword in my js, but I'm also on a team of C++/Ruby programmers who are forced to write js, so the more I can do to make it more familiar, the better. As you said, it's just a tool. Some people like nails and some people like screws; there are objective advantages to each, but either one will hold a house together.
- dehrmann 5y ago> I once heard someone tell me that good OOP ends up looking very functional. Immutability and thinking about if a class should really be stateful are the major wins. I was recently looking over some code that essentially just did transformations, but all the inputs and outputs were member variables, so it took some effort to track what creates what and what depends on what. The members also had empty defaults, so if you mix up the order you call the functions in, the code still appears to work. It wasn't "wrong;" it was hard to follow and error-prone.
- rcgorton 5y agoHmm. Dumb, Dumber, and Dumberer
- zanethomas 5y agoI can't remember the last time I even considered using inheritance in javascript ... except to consider it unnecessary and strewn with mines.
- esturk 5y agoThis seems unlikely. Almost every modern Javascript framework uses it. ie) class MyComponent extends React.Component {}
- neallindsay 5y agoEven React is de-emphasizing class inheritance in favor of more function components.
- simonlc 5y agoYeah and with hooks they arent even necessary at all anymore.
- jakelazaroff 5y agoWith one caveat: you still need class components for error boundaries. https://reactjs.org/docs/error-boundaries.html https://reactjs.org/docs/error-boundaries.html
- mastrsushi 5y agoI've never developed for the web out of fear of talking employers into recruiting me as a UI/UX designer. I've also been pretty thrown off over Web Development being known as a massive mess. I've worked on desktop applications in C# so far. Is web development anymore pleasant or is it all just muckwork?
- schwartzworld 5y agoI develop front-end, mostly React, and I find it to be a lot of fun. The large community has resulted in some amazing tooling, and the unopinionatedness of React means I can write code in the way I like. > I've never developed for the web out of fear of talking employers into recruiting me as a UI/UX designer. I do think that as developers we need to help with design in some limited ways. Often designers will miss things. How is this going to look at different screen widths? Is this accessible? Does this function the way users expect websites to function? I give quite a bit of feedback to designers I work with. But nobody who ever saw me design something from the ground up would try to get me to do it full-time.
- pr0crastin8 5y agoAs someone quite in love with functional programming (background in Haskell and Scala), one thing that seems quite obviously missing here and in my Javascript experience so far is the use of typeclasses as a form of ad-hoc polymorphism. Is there a nice (i.e. not too much boilerplate) way of using them in the land of Javascript/Typescript?
- chrischen 5y agoSee https://github.com/gcanti/fp-ts https://github.com/gcanti/fp-ts. Heavily haskell-inspired and includes a TS implementation of higher-kinded types.
- samhh 5y agoThis is definitely the best option in the TS ecosystem but it's not meaningfully implementing typeclasses from the perspective of the end user. There's no single `fmap` for example, instead you'll be using dedicated `Array.map`, `Option.map`, etc.
- shirogane86x 5y agoAs far as i remember there's functions that take explicit typeclass dictionaries (that you can compose via contramap). So I guess they sort of do typeclasses, but without implicits, which is pretty painful IME. Example: https://gcanti.github.io/fp-ts/modules/Functor.ts.html#map https://gcanti.github.io/fp-ts/modules/Functor.ts.html#map
- megamix 5y agoI write all my scripts top-down so I need not to think about complex stuff and also divide on which method is the best because there's always another programmer who has a different opinion.