Proper Javascript Classes
Javascript is an odd language, especially if you are used to programming in languages like PHP. For example, take a look at how you would create a parent class in PHP followed by a child class that inherits from it:
<?PHP // A simple class in PHP class Footware { public $color; public $size; function __construct($color, $size) { $this->color = $color; $this->size = $size; } function showColor() { echo $this->color . "<br/>\n"; } } // Another class that inherits from Footware class Boots extends Footware { public $style; public $laces; function __construct($color, $size, $style, $laces) { $this->color = $color; $this->size = $size; $this->style = $style; $this->laces = $laces; } function showUse() { switch ($this->style) { case 'Steel Toed': $alert = 'Used for work'; break; case 'Cowboy': $alert = 'Used for line dancing'; break; case 'Fashion': $alert = 'Used for looking good'; break; default: $alert = 'Not sure what you would use these boots for'; } echo $alert . "<br/>\n"; } } $myShoes = new Footware('Tan',11.5); $myBoots = new Boots('Red', 11, 'Cowboy', 0); $myShoes->showColor(); $myBoots->showColor(); $myBoots->showUse(); ?>
Classes are clearly defined, both visually and functionally. Inheritance is also easy to see and implement. Now take a look at the Javascript implementation of the PHP code above:
<script style="text/javascript"> // Parent class definition function Footwear(sColor, fSize) { this.color = sColor; this.size = fSize; } // Use prototype to add methods Footwear.prototype.showColor = function () { alert('Color is: ' + this.color); } // End of the parent class // Child class definition function Boots(sColor, fSize, sType, iLaces) { // inherit properties from the parent class Footwear.call(this, sColor, fSize); this.style = sType; this.laces = iLaces; } // ...and use prototype chaining to inherit methods Boots.prototype = new Footwear(); // Add more methods afterwards Boots.prototype.showUse = function () { switch (this.style) { case 'Steel Toed': alert('Used for work'); break; case 'Cowboy': alert('Used for line dancing'); break; case 'Fashion': alert('Used for looking good'); break; default: alert('Not sure what you would use these boots for'); } } // End of the child class var oMyShoes = new Footwear('Tan', 11.5); var oMyBoots = new Boots('Red', 11, 'Cowboy', 0); oMyShoes.showColor(); oMyBoots.showColor(); oMyBoots.showUse(); </script>
This is just one way to do it in Javascript but in general this is the best approach. Essentially what your doing is creating the class in two steps, a constructor portion (the function ClassName (param) portion) which contains all your properties, followed by a prototype section which houses all your methods.
While visually, this hybrid approach, makes a class look like it’s not encapsulated, rest assured it is. This method also gives you single parent inheritance, good memory usage, and fewer unexpected behaviors vs using just a constructor paradigm (good if you need multiple inheritance), or a prototype method by itself.



