11

At our company we have pretty large body of PrototypeJS based JavaScript code, which we are porting to jQuery for several reasons (not really important here). I'm trying to set up coding guidelines to make/keep things tidy during the porting.

One observation from wading through the prototype based implementation is that a lot of code is written in OOP style (using Prototype's Class.create), but the code is not "object oriented" in spirit. The general pattern I've seen: one "constructor" which you are expected to call (but not call twice, because the constructor uses hardcoded DOM id's), a bunch of other "functions" and event handlers, which you are not expected to call (but the because there is no "private" in JavaScript, you don't know that) and data sharing between all these functions through this. Seen from the caller's point of view there is just one "callable" and nothing else.

I'm starting to believe that OOP in JavaScript can and maybe should be avoided in a lot of cases. At least for our use case, which is not the next generation Goole Wave UI, but simply put: a bit of AJAX based enhancements, registering some event handlers here and there, minor DOM manipulations and such.

  • the functionality we need seems to be implementable just fine in the typical jQuery non-OOP way, using closure magic to obtain encapsulation and privateness. As side effect, the minification efficiency of the ported code is much better.
  • OOP in JavaScript is non-traditional and confusing if you are coming from a "traditional" background. There are a lot of attempts and frameworks to approximate traditional OOP, but IMHO this makes things even more confusing and fragile.
  • One thing that Crockford's "JavaScript the good parts" taught me, is that the true power of Javascript lies in function scopes and closures, much less in OOP patterns.

I'm wondering if there is wider support for this feeling that OOP in JavaScript doesn't really cut it as the sacred mantra, like it is in other languages/platforms. And, in extension, what kind of non-OOP/closure based patterns are much more preferable.

Stefaan
  • 210
  • 3
    Javascript does its "OOP" using functions and closures. Private methods are realized by declaring them the right way in the right scope. Yes, it's different, but it's possible. It doesn't have to be "sorta OOP", you just need to apply it correctly. In fact, it helps to wrap your head around Javascript's way of OOP, since it gives you a new perspective on what "classic OOP" in other languages is and how little you actually need to implement it; one language construct (function) is sufficient. – deceze May 15 '13 at 15:59
  • 1
    @deceze That would make a decent answer, if you added a short code sample. – Robert Harvey May 15 '13 at 16:06
  • @gnat: The first one, maybe. Not the second. I think the OP is asking the opposite. – Robert Harvey May 15 '13 at 16:09
  • @Robert I hereby give permission to use that comment as answer, if a code sample is added. I'm not fluent enough in Javascript to quickly whip one up correctly at the moment, as I'm a little busy. :) – deceze May 15 '13 at 16:09
  • @RobertHarvey as long as the opposite is exact, this shouldn't matter don't you think? I mean, good, correct, thorough answer to particular question would naturally cover it's exact opposite – gnat May 15 '13 at 16:12
  • @deceze I consider closures to be the "inverse" of OOP: in OOP you inject functions in the data container/struct and with closures you inject data in the function (scope). – Stefaan May 15 '13 at 16:13
  • 1
    @Stefaan "Classes" are containers that data is injected into which is only accessible to certain functions ("methods") within that container. This can be modeled perfectly using functions and aspects of closures, it's the same idea. Maybe you're thinking of deeply nested trees of callbacks within callback; if so nobody said that's either good or OOP. – deceze May 15 '13 at 16:16
  • @deceze Exactly. That's the pattern we here are converging to at the moment, I think. But I'm hesitant to call it "OOP". The thing that makes me hesitate is that there is not really an clear tangible "object/container" to be oriented around. Unless you consider the memory associated with the current scope hierarchy to be the "container", but that's not as well/clear defined on the level of source coude as it is in "traditional" OOP. – Stefaan May 15 '13 at 16:31
  • @deceze well you do lose inheritance, generic methods and the ability to have a program that creates a lot of objects. That's a lot when you can just use underscore prefix/suffix. – Esailija May 15 '13 at 17:22
  • to you for getting away from Prototype and into JS.Net ... erm, I mean jQuery! It's power level is "Over 9000!"
  • – SpYk3HH May 24 '13 at 14:40