Which Object Oriented Javascript style is preferred?

I’m researching OO for Javascript and I’ve come across many fragmented opinions. Some developers argue for more a more pseudo-classical approach while others opt for prototypal. I am slowly reaching the conclusion that pseudo-classical is preferred BUT that still seems fragmented. I have seen multiple approaches to pseudo-classical OO JS.

Is there a preferred or industry standard approach to OO JS? Or is that fragmented across the industry? I ask because i want to study the “proper” approach to OO in JS… if there is a proper approach.

The use of pseudo-classical tends to occur with people that are coming to JavaScript from other languages. Those other places aren’t capable of doing what prototypal can do, so they don’t know of the benefits that it can bring.

Prototypal inheritance on the other hand is fully capable of simulating the pseudo-classical techniques, as well as having its own prototypal-based benefits.

Are you dealing with a team that only knows the pseudo-classical approach? In that case they may be faster applying what they already know.

If on the other hand you want to make use of the full power of JavaScript, start taking a good look at the prototypal techniques.

Some useful resources include:

2 Likes

I have a broad understanding of OOP and have mostly employed in a language called Processing which is built on Java. OOP is great. However as I’ve dived deep in JS it’s evident that it doesn’t really quite do OOP well and the contortions it takes to create a pseudo-classical style are sometimes tedious.

My point is that I actually do prefer a prototypal approach in JS but I suspect the industry does not and I’m wondering if anyone has input on that. I’m studying JS actively for the sake of getting a new job. I don’t currently use JS regularly in my work. If it were up to me, I would stick with prototypal but I want to optimize my studying for industry needs.

Unfortunately “the industry” has no clear-cut preference. The techniques that are used depend entirely on your own personal preferences, and on any in-house style-guides that a team might have.

About the closest that we can get in terms of finding out what is commonly being done, are things like the 2014 JavaScript Developer Survey, where the summary analysis (PDF) provides some interesting (though no OO breakdown) details.

It’s useful to know both the OO and prototypal techniques, so that you can use either/or as the case may be, and use your own preferred coding style when it comes to your own personal preferences.

I’m with you in finding that JavaScript is better suited to prototypal myself too.

My impression is the same.

Once upon a time, pseudo-classical was the “official” way to do OOP in JS. Then some smart and influential people introduced us to the way JavaScript’s inheritance mechanism actually works, and for several years prototypal was very much in vogue. But I think the novelty is fading, and people are drifting back to pseudo-classical. I think the new ES6 class syntax is going to reinforce this.

I’ve been to prototypal and back, and ultimately I think pseudo-classical better. What’s touted as prototypal’s greatest feature is, in hindsight, something I think best avoided. In prototypal, “types” are actually concrete objects, which means we can alter and augment them during runtime. But, to do so seems to me to be bad practice, for the same reason that to alter or augment the built-in prototypes (Array, Date, etc) is also bad practice – because it can lead to unpredictable behavior. The exact same object could behave differently depending on if, and even when, its prototype is altered. That kind of unpredictability is bad. And if we choose to restrain ourselves from altering prototypes, then the feature set we’re left with is our emulation of classical inheritance.

Although I think we’re trending toward pseudo-classical, at the moment I don’t think there’s any single approach regarded as “proper”.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.