Saturday, March 31, 2007

JavaScript Inheritance - dojo declare and mighty hitch

Last week I was desperately searching for examples how to do true inheritance in javascript. I found many ways and was little confused why any of them didn't work for me (the problem was actually elsewhere :-x).
The best solution I found was Object Oriented Super Class Method Calling with JavaScript written by Joshua Gertzen.
There is a nice survey about JavaScript inheritance techniques at Ajaxian.com.
Yahoo! User Interface uses Kevin Lindsey technique and there are more I came accross in my search:
javascript.crockford.com
phrogz.net/JS/Classes/OOPinJS2
weblogs.asp.net/mschwarz
dean.edwards.name/weblog

At last I settled for solution provided by dojo because I thought that it would solve problems I had had with other solutions. Well I was wrong, the dojo.declare work the same way as previous solutions I had tried. So I started to dig deeper and deeper to JavaScript and dojo sources and found magic dojo.lang function hitch. This function runs the function in given context and that was all I needed. Every time I tried to call method this.myMethod and debugger showed myMethod is not a function or not defined I knew a I had to use precious hitch.

At start I chose dojo because it seemed to be very powerful and I was right, but could be better documented. Now I know that if I want figure out something, I have to look in dojo sources that are documented quite well.

No comments: