Re: Prototype OOP example

Cliff Mees
Sat, 24 Dec 2005 21:10:49 -0000

Here''s one approach:

Labour = Class.create();
Labour.prototype = {
   initialize: function(name) {
     this.name = name;
   },
   showMeYourName: function() {
     alert(this.name);
   }
}

Worker = Class.create();
Worker.prototype = Object.extend(Labour.prototype, {
   initialize: function(name, position) {
     this.name = name;
     this.position = position;
   },
   showMeYourPosition: function() {
     alert(this.position);
   }
});

var labour = new Labour(''Bob'');
labour.showMeYourName();

var worker = new Worker(''Joe'', ''Boss'');
// call "base" class method
worker.showMeYourName();
// ... and invoke new Worker class method
worker.showMeYourPosition();


This just replaces the existing initialize with a derived version.
There''s no provision to call Labour.initialize in Worker.initialize.
You do get everything else though...

Cliff


On Dec 24, 2005, at 14:49, QnA wrote:

> Hi,
>
> Here is what I want to do:
>
> Labour = Class.create();
> Labour.prototype = {
>     initialize:function(name){
>         this.name = name;
>     }
> }
>
>
> What I want to do is create a class called "Worker" which will
> inherit from "Labour", and the signature of
"initialize" is
> "function(name, position)".
>
> May I ask what should do?
>
>
> Thank you all very much for the kindly help.
> Wish you all with a Merry Christmas.
>
>
> Regards,
> QnA