JavaScript IS Object Oriented Programming

I spent a good half hour writing an answer to a question on Quora today and I thought it was good enough to qualify as a blog post. The question was How do you implement object orientation in JavaScript?

I’ll be honest, I was a little disappointed by the answers posted prior to mine, however that just fueled the fire and gave me good reason to thoroughly and properly answer the question. As always, I like to let the code do the most talking.

This was the answer I posted:

The real answer is JavaScript IS object oriented already, without any “help” from libraries. It seems that most developers simply don’t get it, and cannot get past the concept of traditional “Classes”. Javascript is a prototypal, object oriented, instance based programming language. In JavaScript, everything is an object that can have properties and methods (sounds familiar, right? maybe like… “classes”?). Behavior and characteristic reuse is accomplished by extending, or “decorating” an existing object; the existing object serves as a “blueprint” or more literally, a prototype.

In JavaScript, traditional classes do not exist, however the equivalent is surprisingly simple and straight forward, all you need is a function(){}:


//  Posted at:

function SafetyBlanket() {
  this.isObject = true;

// Declare and assign a new instance
var blanky = new SafetyBlanket();

console.log(blanky); // `object definition`

To be really, really picky…


console.log(blanky); // `object definition`
console.log(typeof blanky); // object

console.log(blanky.__proto__); // object
console.log(typeof blanky.__proto__); // object

console.log(blanky.constructor); // SafetyBlanket()
console.log(typeof blanky.constructor); // function

console.log(blanky.constructor.prototype); // object{}
console.log(typeof blanky.constructor.prototype); // object

// Notice it always comes back to an object

// ------------------------

console.log(blanky.isObject); // true
console.log(typeof blanky.isObject); // boolean

console.log(blanky.isObject.__proto__); // object
console.log(typeof blanky.isObject.__proto__); // object

console.log(blanky.isObject.constructor); // Boolean()
console.log(typeof blanky.isObject.constructor); // function

console.log(blanky.isObject.constructor.prototype); // false {}
console.log(typeof blanky.isObject.constructor.prototype); // object

// Again, it always comes back to an object

The “class” or object instance can be extended/decorated:


function SafetyBlanket(material) {
  this.isObject = true;
  this.madeOf   = material;

// Extend the prototype with a new method
SafetyBlanket.prototype.tuckIn = function() {
  return this.madeOf;

// Declare and assign a new instance
var myBlanky    = new SafetyBlanket('silk'),
    yourBlanky  = new SafetyBlanket('fiberglass');



The “class” or object instance can be inherited:


function Developer(lang) {
  this.isObject = true;
  this.prefs    = {
    lang: lang

Developer.prototype.getPrefs  = function () {
  return this.prefs;

function FrontEnd() {}
function BackEnd()  {}

FrontEnd.prototype  = new Developer('javascript');
BackEnd.prototype   = new Developer('python');

// Reassign the constructor to reflect itself
FrontEnd.prototype.constructor  = FrontEnd;
BackEnd.prototype.constructor   = BackEnd;

// Extend the prototype with a new method
FrontEnd.prototype.getDOMWindow = function () {
  return window;

// Extend the prototype with a new method
BackEnd.prototype.getInterpreter = function () {
  return this;

// Inspect them now, they have the characteristics of
// the Developer object as well as their own methods

// Declare new instances of our two objects
var frontEndDev = new FrontEnd(),
    backEndDev  = new BackEnd();

// To be sure, run their methods

As you can see, JavaScript doesn’t need any libraries to “implement object orientation”, as it is VERY capable on its own.

If you want to play with this code, I’ve posted a Gist at Github:

Edit Thanks to Dmitry for reminding me of his excellent resource


We moved off of Disqus for data privacy and consent concerns, and are currently searching for a new commenting tool.

  1. Very nice little synopsis! –> Refresh my memory: Why have people argued that JS isn’t an object oriented language? No need to say \”public…\” or \”… extends Object\” ?

  2. Good explanation.

    Moreover, as I mentioned, difference \”class\” vs. \”prototype\” is not so essential. To see the complete difference of prototype based model, it’s needed to consider all other properties (statics, characteristic resolutions, whether classes/functions are \”first-class\”, etc.) of OOP models.

    E.g. you won’t find a big difference between Python’s classes (which has dynamic \”first-class\” classes and delegation model for properties/methods resolution) and delegation based prototypes of ECMAScript. From this viewpoint, classes in Python are just syntactic sugar which is achieved in ECMAScript using wrapper-functions to get a useful code-reuse (to chain prototypes).

    If interested more deeply in this general theory, take a look:

    And regarding OOP exactly in ECMAScript:


  3. I usually start to cry when people use \”object oriented\” and javascript together, but you nailed it. Well done.

    What you could do is in the constructors of the subclass call the parent class and apply the current object. Like a super() call.

    function FrontEnd(lang){, lang);

Contact Us

We'd love to hear from you. Get in touch!


P.O. Box 961436
Boston, MA 02196