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:
- Don’t create any plain old JavaScript objects (functions, arrays etc. are allowed)
- 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
myObject('doSomething')(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!
Hi Dear, are you really visiting this webb page regularly, iff so then you will definitely obtain nice knowledge.
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’
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
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
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
Great post.
http://www.royalasscher.jp/ninaricci/ea1a4-54084-8.htmlニナリッチ 財布 小銭入れ,ニナリッチ 時計 修理£¬ニナリッチ 香水 アーモンド
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.
Thanks for finally writing about >Skilldrick » Making inheritable objects in JavaScript without objects <Liked it!
WOW just what I was searching for. Came here by searching for business gas comparison
Hi, every time i used to check weblog posts here early in the break of day, as i love to learn more and more.
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.