Had you invested in low-cost index funds instead of doing what you did, would you have more or less? How much more or less?
GreaterThanZero.com


Page 2 of: Object-Oriented Programming in JavaScript Explained, by Thomas Becker   about me  

Introduction

As of this writing, Wikipedia describes JavaScript as “a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles.” Perhaps a better description would be, “JavaScript is a functional programming language that supports object-oriented programming as an optional feature.” JavaScript is a functional language1 by its very nature; there is no way to escape that. However, one can program in JavaScript without even knowing anything about object-oriented programming. Of course, one would still be using the term “object” a lot. But that's only because one of the two main data structures in JavaScript is called object (the other one being the array). When you create a JavaScript object literal, like this:
var point = {x: 0, y: 0};
then you're just using a certain data structure, namely, an associative array, or dictionary, which is implemented typically, but not necessarily, as a hash map. This is a far cry from what most people consider object-oriented programming. There is nothing wrong with limiting yourself to object literals, perhaps in conjunction with the module pattern. You just need to know that in doing so, you're barely scratching the surface of "real" object-oriented programming.

In traditional object-oriented languages such as Java and its predecessors, the core concepts are classes and their instantiations. JavaScript, by contrast, does not have the notion of a class. It relies on an entirely different mechanism, namely constructor functions and prototypes, to support object-oriented programming. This tutorial explains that mechanism and provides a hands-on recipe for moving from the traditional class-based approach to the JavaScript way.

There are JavaScript libraries that encapsulate the constructor/prototype mechanism and make JavaScript look more or less like a class-based language. The Prototype library, for example, has a method called Class.create. It is debatable whether it is a good idea to emulate an idiom like class-based OOP in a language that was not designed to support that idiom. Personally, I prefer to use the idioms that the designers of the language provided for me to use (constructor functions and prototypes in this case). But of course that is a matter of discretion. If the approach of a library like Prototype works for you, then you should use it.

If you are coming from the “strict” OO tradition of languages like C++ and Java, you may find JavaScript's OO facilities lacking because JavaScript does not have access specifiers like public and private, a trait that it shares with many modern dynamic OO languages such as Python. This is a problem especially for those who tend to use class hierarchies extensively, as the lack of access specifiers exacerbates the fragile base class issue. But if you are like me and use the OO paradigm casually, as nothing but a convenient tool to group data and the functions that operate on it, with the occasional inheritance thrown in for good measure, then JavaScript will serve you well.

My favorite source for the topics discussed here is David Herman's Effective JavaScript. In fact, the only reason why I wrote this tutorial is that David Herman's book, as its title indicates, is written Scott-Meyers-style as a collection of items, each giving advice on a narrow topic. I believe that this leaves room for the recipe-style presentation that I am attempting here.


1When I say “functional language,” I don't mean that in the strict, pure sense of languages like Haskell. Rather, I am referring to to the more general meaning of the term, where a language is considered functional if all functions are first class objects (which implies support for higher order functions) with closure.