Posts Tagged ‘OOP’

Object Oriented Programming in JavaScript

Tuesday, December 14th, 2010

(From A Student View)

When an institute is teaching programming to new students, it’s a common theme to play around with Java before going on to other languages. It’s easy to learn, has all the Object-Oriented goodies instructors want, and is fairly ubiquitous.

With the current snafu that is Oracle circling around Java and all the rest of Sun’s old open-source projects, I decided I would try my hand at emulating those same principles using JavaScript; an open, simple, ubiquitous technology.

As it turns out, the resemblances end with those adjectives. JavaScript was never really meant to do the things we’re making it do now. It’s possible to do most things in most languages, but the verbosity of the code needed to perform a specific action tells you what it’s made to do, and what’s been tacked on as an after-thought.

So, from a student’s point of view, how does it matter which language they’re using? Which is easier to learn, or simpler?


  • Requires each class to be in its own file. It can be tedious, but it can also make it easier to manage.
  • Each file has one class. Each class has a constructor (sometimes overloaded), and zero or more methods. The structure is fairly simple.
  • Besides the keywords, not much is being used. If you don’t import anything, there are very few class names already taken.
  • It’s made to work with the console, or to display a GUI.
  • Inheritance is an important part of Java, and all you need is one keyword and the class name to inherit.


  • Everything can go everywhere, and the syntax can go different ways. This freedom can be devastating to those who are learning.
  • Objects are functions, and functions can go within functions, and what are closures, anyway? Where and how you create variables influence how you can access that variable later in the program (private, public, hidden, privileged, for either fields or functions).
  • The global scope is chock full of different classes and attributes and functions.
  • It’s not meant to do anything on its own. You have to teach students HTML and DOM manipulation at the same time, and programs have to output in HTML.
  • Inheritance is handled through the prototype chain, which has to be explicitly set after an object function is created. You have to set the prototype of that class to a new instance of the parent class, and then you have to set that prototype’s constructor back to the constructor of the current class (to ward off some edge cases).

I’ve made a sort of console window out of HTML. It overwrites the document.write(), prompt(), and confirm() methods to output to a <pre> element. The latter two also pause execution by opening an invisible modal dialogue that is closed when the Enter key is pressed, which enables synchronous user input.

It’s a cludge, but you can run any JS code in there that acts like a console Java program.

Later: GUIs.

In short, You can do Object-Oriented programming in JavaScript, but it’s not something you should be teaching students in their first semesters, and the complexity and power of it don’t lend well to simple instruction.

I’m going to keep playing around with this, to see if I can get something simpler out of it. I just got the ‘instanceof’ keyword working, so I can start chipping away at what I need to get a ‘true’ out of that.