Hi lun.ashis,
the first part, creating the Animal class is OK.
For the Snake class, you'll create a new class based on Animal. So all
Animal methods will be avalaible in the Snake class. This is done by
this part:
var Snake = Class.create(Animal, { ...});
Next, you want to modify the Initialize constructor (which will be
called at the creation of a new instance of this class called by ""new
Snake(...)""). You still
want to be able to call the original function from the Animal class.
That is what is done with the redefinition of the Initialize function.
Now first parameter is the variable that will contain the reference to
the Animal "Initialize" method.
Second parameter is the option you push to the Snake constructor.
So creating a new snake is done like that:
var _python=new Snake('python');
At this moment the initialize class of the Snake class will just call
original Animal Initialize method with the parameter 'name' and a
predefined sound ==> all snake will have the same sound.
Now the original function just save the namez & sound variable inside
the newlly created object and when calling the Snake.speak method, it
will alert with the way snake speak.
For the given example:
var _python=new Snake('python');
_python.speak() ==> alert('python says:"hissssssssss"');
Hope that help !
--
david
On 5 juin, 08:27, david <[email protected]> wrote:
> Hi lun.ashis,
>
> the first part, creating the Animal class is OK.
> For the Snake class, you'll create a new class based on Animal. So all
> Animal methods will be avalaible in the Snake class. This is done by
> this part:
> var Snake = Class.create(Animal, { ...});
>
> Next, you want to modify the Initialize constructor (which will be
> called at the creation of a new instance of this class). You still
> want to be able to call the original function from the Animal class.
> That is what is done with the redefinition of the Initialize function.
> Now first parameter is the va
>
> On 5 juin, 05:59, "lun.ashis" <[email protected]> wrote:
>
> > In prototype i found these example of the class inheritance.
>
> > var Animal = Class.create({
> > initialize: function(name, sound) {
> > this.name = name;
> > this.sound = sound;
> > },
>
> > speak: function() {
> > alert(this.name + " says: " + this.sound + "!");
> > }
>
> > });
>
> > // subclassing Animal
> > var Snake = Class.create(Animal, {
> > initialize: function($super, name) {
> > $super(name, 'hissssssssss');
> > }
>
> > });
>
> > Can anyone explain me this.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---