Friday, May 29, 2009

Javascript Inheritance

When I first started embarking on larger scale JavaScript projects (not just the standard event handler snippets) it took me a while to adjust to the differing inheritance style since i was used to class based programming. Initially I flirted with the Microsoft AJAX.NET approach, which attempts to bolt on a class based, strongly typed inheritance model onto JavaScript (even to the point of mimicking enumerations and interfaces), but eventually I realized that it was best to use the language the way it was intended to be used.

After a bit of searching among the variety of approaches out there, I ended up using the parasitic inheritance model proposed by Douglas Crockford. The essence of this approach is having the constructor for a subclass instantiate an instance of its superclass, modify it by adding methods, and then returning the modified object. A simple inheritance would look like this:




   1:  car= function(manufacturer) {

   2:      // class we inherit from

   3:      var me = new vehicle(manufacturer);

   4:      me.honkHorn = new function() {

   5:          // do stuff

   6:      };

   7:      return me;

   8:  }

   9:   

  10:  var myBlueVolvo = new car= ("Volvo");



This seems pretty natural and gives most of what I was looking for (code reuse and some variable hiding) without torturing the language too badly.

I do occasionally need to call methods in the superclass from the subclass though. Douglas implements a relatively complex "sugar" that automatically provides reference to a "uber" object that mimics the "base" object in C#, but I was worried that this was a lot of extra code for something I really only do with a handful of functions. Complexity is bad.

Instead, when I need to reference an overridden method in the superclass, I took advantage of JavaScript closures to provide a simple way to call a superclass method. Here is an example:



   1:  var super = me.honkHorn; 

   2:  me.honkHorn = new function() {

   3:      // call the method on the superclass

   4:      super.honkHorn();

   5:   

   6:      // now do stuff in the subclass

   7:  };



By saving the superclass function in the super var, it gets captured and is still around to be called, even though it is no longer references by the new object as a public function.

1 comment:

sandhirstdagher said...

On each play table you must to} see a chart that reveals how many of} coins you'll receive when successful a certain hand of poker. If you play five coins on a Jacks or Better table you can get get} 45 coins for a full house as a substitute of 9 coins. You additionally will receive a bonus for Royal Flush with a max guess. Video poker gamers actually have one of the best of each worlds. Poker is the preferred card game on the planet, and slot machines are the most important factor on the casino. For over 40 years, poker followers have enjoyed the ease and comfort of enjoying in} the machines, whereas slots followers have enjoyed the added skill component 벳메이트 behind video poker.