Home > Programming > Making inheritable objects in JavaScript without objects

Making inheritable objects in JavaScript without objects

August 14th, 2011

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

  5. June 23rd, 2015 at 02:18 | #5

    Financial MattersRead on to discover how to find the perfect resort and plan your worktime properly.Buying wedding resort that blends ceremony and reception with convenience and activities, all for 1 upfront price? Consider a detailed inclusive. Read on to learn Polo Ralph Lauren Factory Store how to find the optimal resort and plan your mood properly.10 suggestions for perfect wedding welcome bags10 strategies for perfect wedding welcome bagsWedding welcome bags are a fun way to greet your frie

  6. July 30th, 2015 at 21:20 | #6

    Great post.

  7. August 18th, 2015 at 08:47 | #7

    http://www.royalasscher.jp/ninaricci/ea1a4-54084-8.htmlニナリッチ 財布 小銭入れ,ニナリッチ 時計 修理£¬ニナリッチ 香水 アーモンド

  8. September 14th, 2015 at 06:03 | #8

    A colonoscopy would be the most precise way for creating a correct diagnosis of [ulcerative colitis](http://www.kippertool.com/forum/discussion/4119/frequent-diarrhea-may-prevent-the-body-from-being-able-to-use-essential-nutrients-this-is-of/p1 “ulcerative colitis”). It’s also not certain how long benefits of using the patch may last. But, be careful with cabbage because it has been known to give many people with IBD problems.

  9. October 7th, 2015 at 20:20 | #9

    Thanks for finally writing about >Skilldrick Making inheritable objects in JavaScript without objects <Liked it!

  10. October 10th, 2015 at 14:38 | #10

    WOW just what I was searching for. Came here by searching for business gas comparison

  11. October 11th, 2015 at 04:18 | #11

    Hi, every time i used to check weblog posts here early in the break of day, as i love to learn more and more.

  12. October 11th, 2015 at 20:37 | #12

    Hi everyone, it’s my first pay a quick visit at this web page, and article is really fruitful designed for me, keep up posting these content.

Comments are closed.