Function as Object
JavaScript uses Prototypal Inheritance in place of the classical OOP class inheritance model.
A class is essentially a Function with a glorified definition hash: prototype. The Function.prototype
object (hash) behaves like a factory providing method signatures for object creations. Within the class' constructor and
instance methods, the variable this refers to the instance object itself.
Function Object
Defining an class is as simple as assigning a function to a varialbe. This object function acts as the constructor /
initializer for the class. By JavaScript convention, the class name is always CamelCased.
var Animation = function( type, speed ) { this.type = type; this.speed = speed; }
Prototype
Instance methods are defined in the function object's prototype hash. By defining Function.prototype every new
object created for that given class will inherit the instance methods specified by its the prototype.
Animation.prototype = { start: function() { console.log('Rendering animation ' + type + ' at ' + speed); // ... render animation ... }, stop: function() { console.log('Animation ' + type + ' has stopped.'); // stop animtion } } var animation = new Animation( 'blind', 1000 ); animation.start(); animation.stop();