Saturday, July 25, 2009

Object oriented JS

I was going th the object oriented js and below is the basic example


function base (){};
base.prototype.foo=function(){
alert('base prototype foo');
};

var b = new base();
b.foo = function(){alert('base.foo');};

function sub(){};
sub.prototype=b;

function subsub(){};
subsub.prototype= new sub();

function sss(){};
sss.prototype=new subsub();

function test(){
//var b = new base();
alert('b.foo\n' + b.foo);
//alert('b.prototype foo\n' + b.prototype);
//var s0 = new sub();
//alert('s0.foo\n'+ s0.foo);
//alert('s0.prototype.foo\n' + s0.prototype.foo);
var ss= new sss();
alert( 'sss.foo\n' + ss.foo);
alert(sss.toString());
//ss.foo();
ss.prototype.foo();
};
test();








base is extended by sub, which is further extended by subsub. We create a new instance of this subsub and call the inherited method. simple.

No comments:

Post a Comment