Home > Programming > Making inheritable objects in JavaScript without objects

Making inheritable objects in JavaScript without objects

For a long time I’ve found the subject of the equivalence of closures and objects fascinating. The problem with creating objects using closures is that there isn’t a way to do inheritance (although you should always favour composition over inheritance anyway!). I thought it would be fun to see if it was possible to implement an object system in JavaScript with inheritance, without using any plain JavaScript objects.

To do this, I set myself two rules:

  1. Don’t create any plain old JavaScript objects (functions, arrays etc. are allowed)
  2. Don’t use the dot operator.

For that to work, I wrote some wrappers for the JavaScript methods I would need, like Array.prototype.slice.call (this becomes callSlice). Apart from those wrappers, there are no dots used anywhere. To dispatch methods based on a string name, instead of using an object (which would have been much easier but would have missed the point of the exercise) I’m passing around functions which dispatch methods using conditional constructs (if, else and switch).

Introducing funcobj!!!

Yeah I know, it’s a rubbish name. Anyway, the Github repo is here and I’ve got a running example on JsFiddle here.

Here’s some examples of how it works:

//call the doSomething method on myObject, with the argument 500

//call the doSomethingElse method on the object's super object
myObject('doSomethingElse', true)();

To make an object like this, call objMaker with a function that defines and dispatches methods:

To inherit, pass a third argument to objMaker, which is the object to delegate to if the new object does not contain the called method (or: if the new object cannot respond to the message). Have a look at this long example in JsFiddle to see how that works:

This is stupid

Yeah, I know. It’s not meant to be used for anything. It’s probably hopelessly inefficient, and the syntax leaves a lot to be desired. But it does work, which was what I was going for. It might be a stupid idea, but it demonstrates the power of JavaScript closures. It also gives the possibility of dynamically responding to unknown messages, like in Ruby’s method_missing.

This is what objMaker looks like. There’s some fairly gnarly code in there, but if you can get your head around what’s doing what, you’ll hopefully learn something new about JavaScript!

Categories: Programming Tags: ,
  1. March 31st, 2014 at 07:32 | #1

    Hi Dear, are you really visiting this webb page regularly, iff so then you will definitely obtain nice knowledge.

  2. September 24th, 2014 at 10:57 | #2

    Määrät ovat joskus määräytyy valtion virasto, joskus tuomioistuimen ja voidaan sulkea lailla. Se ei ollut Belstaff Rotterdam helppoa murenemassa kentällä, mutta batsmen oli vain syyttää itseään omaan kurittomuus, kuten oli laita Länsi-Intiassa, ja pelkurit, koska se oli Zimbabwen kanssa.. Ei siksi, että ‘omg täysin edustaa yksi tärkeimmistä etujen se on ikään kuin he tekivät koominen minulle!: U’

  3. October 14th, 2014 at 01:47 | #3

    It all started back in 2012 when I developed ovarian cysts on my ovaries that were so distressing, it was unbearable. One of them actually got to the size of an lime, talk about painful, you have no concept. At first, I didn’t know what occuring with me, I would have sex i would have to be rushed to the clinic on several occasions given it became so painful New Balance Singapore that we had to literally lay from the fetal position.The first time I went to my doctor, he injected liquid hydrocodon

  4. October 30th, 2014 at 01:16 | #4

    In dat opzicht denk ik dat het boek is zijn belangrijkste werk doet u think.The zwakste punt van het boek is ook in relatie tot de definitie. Ongeveer drie maanden later, de archeoloog Pei Wenzhong ook op de hoogte Hu dat alle Peking Man fossielen zouden worden verscheept, en Hu werd verteld om verdere instructies af te wachten .. Ik zal zien of ik kan vuur af een email naar de presentator. Mechanisme vaardigheden is belangrijk voor het kind developement teherefore je speelgoed collecties is de

  1. October 10th, 2014 at 02:41 | #1
  2. November 7th, 2014 at 18:01 | #2

Comments parsed as Markdown.