3

I used to program in Java. As a result of that I got well-versed with Object Oriented Paradigm. In my opinion it is a great approach to programming because it's easy to unit test, use design patterns, and so on.

I am now programming in JavaScript. Should I approach JavaScript with an Object Oriented mindset, too? It seems to me that the answer should be "yes", but I want to hear other people's opinion.

4
  • see also: What is the path to JavaScript mastery?
    – gnat
    CommentedDec 19, 2014 at 15:44
  • JavaScript also partially supports programming in a functional style, which you should also consider.
    – Doval
    CommentedDec 19, 2014 at 16:00
  • 5
    When coming from language X to language Y, it is best to keep an open mind. You'll see some patterns that you already know, but also some things that are new. But don't try to use Y as if it was X with different syntax. While JS has extremely rich (but slightly confusing) support for object orientation, it is best not to force Java-like OOP onto it. Also, look out for the functional programming parts – this is where JS really shines.
    – amon
    CommentedDec 19, 2014 at 16:01
  • It's only confusing if you expect it to be like statically-typed OO.CommentedDec 19, 2014 at 20:47

4 Answers 4

7

JavaScript is a multiparadigm language that you can approach from a pure OOP point of view, but most people don't, for various reasons I'll describe in a bit. In my experience, idiomatic JavaScript tends to be around 50% functional, 30% OOP, and 20% procedural. However, that mix varies a lot depending on what you're doing and who you work with.

Some reasons OOP isn't used as heavily in JavaScript as you might expect:

  • The this pointer has weird scoping rules that complicate things.
  • Programmers from the Java/C#/C++ schools aren't accustomed to prototype-style objects.
  • Programs tend to be very asynchronous and callback-heavy, which lends itself very well to a functional style.
  • Functional capabilities of the language obviate the need for many OOP design patterns.
  • The smaller, more self-contained, single purpose scripts lend themselves well to a procedural style.
3
  • 1
    The scoping rules for 'this' are perfectly sensible; the real problem with them is few people have read the spec or understand Javascript's scoping rules well enough.CommentedDec 19, 2014 at 17:00
  • 2
    @ElfSternberg There's nothing sensible about a magic variable in an otherwise lexically-scoped language. I can't think of any other language that passes arguments to callbacks through a magic variable instead of explicitly through the argument list.
    – Doval
    CommentedDec 19, 2014 at 21:05
  • Anaphoric lisp?CommentedJan 15, 2015 at 21:11
1

Javascript is more different from Java than a surface comparison might suggest. An object-oriented approach can get you a long way, but it is not in itself sufficient to become a good javascript programmer, because a few of the critical patterns of javascript are not really object-oriented in nature. You will find that judicious use of closures is as important as any OO design pattern, and that a firm grasp of the "continuation function" pattern is almost as critical.

    1

    I'd recommended a great book JavaScript: The Good Parts by Douglas Crockford as a good guide. JavaScript AFAIK does not have (in practice) a single approach to object-orientation. Also as mentioned in previous answers it is probably more useful to learn functional paradigm mindset, unless you are going to design large frameworks like OpenLayers, jQuery or Node.js.

    One more factor to decide is whether you will work alone or in a larger team. In the latter case it is probably wise to have common programming style.

      0

      Absolutely! When I write javascript, I mainly write objects. Javascript has a wonderful, subtle, IMHO elegant object model that equates function instances, maps, and objects. So you can take advantage of each of those semantics. In practice, I end up wrapping all my code in objects, not because of my Java experience, but just because it allows for nice modularization.

      Also, because of the interesting equivalences between functions, maps, and objects, it may be a lot of folks are using object design without realizing it.

      For example, especially with the modern javascript libraries available, I see lots of code that looks like this:

      { 'thing1': 'blah', 'thing2': function() { ... } } 

      This is an object-oriented approach, because it defines a map (or associative array) which in javascript is the same as an object. (or visa-versa, I forget). The important point it, it bundles data with functionality, which is a core concern of OO.

      Failing that, I see a lot of code rolled up into objects for the purposes of modularization. jquery is heavily object-oriented, and you see lots of extentions like:

      $.zoom = new function() { var privateValue = 0; this.visibleFunction = function() { ... }; }(); 

      Which tacks an object on top of what jquery provides.

      So in my experience, increasingly in the last few years as the javascript frameworks take on an OO aspect, is that OO practice in javascript is really almost the predominant methodology. And, from my experience, no serious javascript developers have given me grief about rolling my code up in objects -- if anything just the opposite.

      (By comparison, that's not as true with in the Python world, where in Python the more natural attitude is dropping in functions at the module level.)

      But the joy I find with doing OO in javascript is that you can leverage the nice scoping / visibility mechanisms it provides to really create clean object semantics in a way that can be really cumbersome in java. And then you get the nice benefits of a dynamic language, like late binding of functions to objects. Also, in Java, reflection is a great set of utilities, but I've always found it really cumbersome. But in javascript, reflection is just a natural aspect of the language.

      So I love taking a break and doing OO in javascript occasionally, because it feels liberating after slogging through Java for a while.

      I think the primary difference with OO in java and javacript is that javascript isn't as obnoxious about it.

        Start asking to get answers

        Find the answer to your question by asking.

        Ask question

        Explore related questions

        See similar questions with these tags.