Recent Weblogs

Links I like

Inheritance and the reclusive function

Two principles that are essential to the developers tool belt, class inerhitance and private methods. Inheritance allows us to write arbitrarily but with an intent, the idea of an abstract class is to roughly define things but leave them open. Private methods let us encapsulate functionality and keep the global namespace clean.

Private Methods

Private methods provide for the ultimate functionality in encapsulation. The idea is that the method has functionality that is only accessible through it, thus bottle necking any specific requests that would require such functionality. These methods are used internally only, but it wouldn't be uncommon, at least in Javascript to have public methods that reference or execute a private function. The benefits to this is approach is that you don't pollute the global namespace with methods specific to a class, you open up your design to better variable integrity through getters/setters.

Class inheritance

Class inheritance is a way to build basic functionality first, the idea is to approach a specific task in a very arbitrary, building abstract classes that are generic to begin with but provide a good base to start to refine functionality into a more specific class. A good analogy is the animal kingdom, let use the example that we had to program a certain breed of dog. We might start with defining a mammal. A mammal would mandate that the creature has eyes and hot blood, but not specific to say what color its fur is or how long its legs are, this is defined at a lower level. Defining the mammal further you could define a the "dog" class, a great base class for a carnal monger. There are so many specific variations of a dog though, in each breed, such as a Shepard, Bernard, Malinoise, Husky etc. The benefit to this approach is that if the example dictated that you now had to develop a cat, you can use the mammal class to build from, thus allowing you to build a plethora of subclasses that all use the same super class.

How to use this in a prototype class.

We can accomplish this using prototype Class.create method and a function closure. Inheritance has been part of prototype for a while but the function closure allows elegant use of private methods, I was introduced to this technique recently in an observable class written by Kangax.

This shows us a pretty advanced approach to an observable class, which is great but I am going to go over some code with a more basic approach to really show you how to inherit properly and what is still available to the subclass.

Animal, The Base Class

In my example I chose to build the base class as an Animal, this way I have basic methods that I can rely on with all subclasses, the default behavior is all returning false, such that if I extend animal with a subclass and need to ensure a different result I will simply have to overwrite that specific method yet leave the others as is, such as with a dog, I'll need to define its legs, but it can simply use its super class's getWings method, dogs don't have wings!

var Animal = Class.create((function(){                
	return {
		getLegs : function(){ return false; },
		getWings : function(){ return false; },
		getFur : function(){ return false; }                                
	};                
})());					
					

The Dirty Dog

The Dog class is just a simple extension, but notice its use of private methods, these are defined in the closure, all Dogs will have these methods available but no subclass will be able to modify them directly, they can overload their public representation but this functionality is secured within the Dog class.

var Dog = Class.create(Animal, (function(){
	function getLegs(){
		return 4;
	}
	function getFur(){
		return true;
	}
	return {
		getLegs : getLegs,                   
		getFur : getFur
	};
})());
					

Busy Bird

The bird class is an example of a more advanced subclass that has the base functionality but adds quite a bit of its own functionality. Private variables and methods for accessing this variable. The public methods only provide a closure to execute the private method, this further encapsulates the private method such that there is simply no public reference to the method.

var Bird = Class.create(Animal, (function(){
	
	function getLegs(){
		return 2;
	}
	function getWings(){
		return true;
	}
	function _initialize(){                    
		var flying = false;
		
		function fly(){
			flying = true;
		}
		function land(){
			flying = false;
		}
		this.beginFlight = function(){
			fly();
		};
		this.endFlight = function(){
			land();                        
		};
		this.isFlying = function(){
			return !!flying;
		}
	}              
	return {
		initialize : _initialize,
		getLegs : getLegs,
		getWings : getWings                  
	};
})());					
					

With these tools available its much easier to build scalable, robust Javascript applications and keep things straight as it grows. Encapsulation is great in the way that it keeps your scope clean of unecessary pollutants. Also it ensures that no other code could be potentially acting upon properties of this class in an unexpected manner, everything is accessed through instance methods so those are like the gates and everything must pass through these gates. In debugging you can "watch" what is executing these methods and quickly find the culprit to your problem.

Resources

Comments are Disabled.