Back in the fall, I watched a talk by Douglas Crockford about javascript and ES6. He said he was no longer using the keyword new in his code because Object.create is here.A lot of things he discussed prompted me to try and better understand what makes javascript javascript. So I explored this Object.create thing and I really like it.

Object.create is a method that will create an object for you and link it to the object that you pass in as the first parameter. As is, javascript is not really “object oriented” like Java because it does not have classes. It has functions that if called with new in front, will return an object which we think is an instance of a class. Javascript is a bunch of objects linked prototypically rather than instances of classes. The notion of classes in javascript is a facade at best. Here is a much better explanation of why classes in javascript are not a good way to write javascript.

The real topic here though is OLOO(aka Objects Linked to Other Objects). I unknowingly started writing my javascript code in OLOO style after the Douglas Crockford talk but only recently did I learn to call it OLOO from Kyle Simpson’s open source book about javascript. (You Don’t Know JS) Basically, OLOO stems from the Delegation pattern which can be seen in javascript prototypes. For instance, rather than doing inheritance like this:

// Traditional 'class' setup
function Person(name) {
  this.name = name
}
Person.prototype.holler = function() {
  console.log("MY NAME IS " + this.name.toUpperCase() + "!!")
}

function Student(name, major) {
  Person.call(this, name)
  this.major = major
}
Student.prototype = Object.create(Person) // need to 'extend' the prototype of Person
Student.prototype.study = function() {
  console.log("I'm studying", this.major)
}
var me = new Student('akonwi', 'business')
me.holler() // MY NAME IS AKONWI!!
me.study() // I'm studying business

we can do this in OLOO style:

var Person = {
  init: function(name) {
    this.name = name
  },
  holler: function() {
    console.log("MY NAME IS " + this.name.toUpperCase() + "!!")
  }
}

var Student = Object.create(Person)
Student.create = function(name, major) {
  this.init(name)
  this.major = major
}
Student.study = function() {
  console.log("I'm studying", this.major)
}

var me = Student.create('akonwi', 'business')
me.holler() // MY NAME IS AKONWI!!
me.study() // I'm studying business

A huge benefit of this is that the model is easy to track. Student is prototypically linked to Person and we know that for sure because of Object.create(Person) creates that link for us. Object.create also allows us to do type checking easily with Person.isPrototypeOf(me), which returns true or false. There are some minor tradeoffs to this way of writing code but I won’t cover that here since it can be found in Kyle’s book. I love this way of modeling inheritance in javascript because it seems more proper and natural but it also resembles ruby in a sense. Using Object.defineProperty, one can create an extendable object that is immutable. Meaning the only way to get an instance of it is to call Person.extend or something like that and it will return a new object linked to the Person object.