inheritance example

by Richard Hunter

JavaScript

function Product(id, name) {
	this.id = id;
    this.name = name;


}
Product.prototype.doThis = function () {
	console.log('do this', this.name);
}

var Food = inherit(Product, {

	__construct : function () {
    	Product.apply(this, arguments);
    },
    
    doSomething : function () {}

});

function inherit(Parent, child) {
	// todo: error check that parent is a function and Child an object
    var F = function () {}
    F.prototype = Parent.prototype;
    child.__construct.prototype = new F();
    child.__construct.Parent = Parent.prototype;
    child.__construct.prototype.constructor = child.__construct;
	return child.__construct;

}
var foo = new Food(1, 'apple');
foo.doThis();