On 19/01/2008, iporter <[EMAIL PROTECTED]> wrote:
>
> Rob, that's a great explanation, thanks.  You're right, I see now how
> using the prototype keyword makes it clearer what is part of the
> class, and what is part of the instance.
>
> In the below example, I have to put the instruction to create the
> behaviours of the Man and Woman in each of their constructors.
> However, Man and Woman are both subclasses of the Person class - how
> can I get this instruction into the Person class?
>
> Cheers again!
>
> ----------------------------------------------------------------------------
> var Behaviours = Class.create({
>         initialize: function() {
>                 this.array=[];
>         },
>         push: function(behaviour) {
>                 this.array.push(behaviour);
>         },
>         list: function() {
>                 $A(this.array).each(function(behaviour) {
>                         alert(behaviour);
>                 });
>         }
> });
>
> var Person = Class.create({});
>
> var Man = Class.create(Person, {
>         initialize: function () {
>                 this.name = 'Iain';
>                 this.behaviours = new Behaviours();
>                 this.behaviours.push('eat');
>         }
> });
>
> var Woman = Class.create(Person, {
>         initialize: function () {
>                 this.name = 'Ailsa';
>                 this.behaviours = new Behaviours();
>                 this.behaviours.push('talk');
>         }
> });
>
>
> On Jan 18, 7:13 am, RobG <[EMAIL PROTECTED]> wrote:
> > On Jan 18, 2:30 am, iporter <[EMAIL PROTECTED]> wrote:
> >
> >
> >
> > > In the below code, I expect two alerts '1 : 1' and then '1 : 1', but
> > > in reality I get '1 : 1' and '2 : 1'.  For some reason, the
> > > declaration of the second object of class myClass alters the first
> > > object of the same class.  However, it only alters
> > > 'this.options.value', and not 'this.value'.  Can you tell me why this
> > > behaviour occurs, and how to resolve it?
> >
> > > Cheers
> >
> > > ---------------------------------------------------------------------------
> > > var myClass = Class.create({
> > >         options:{},
> > >         value: false,
> > >         initialize: function(options) {
> > >                 this.options.value = options.value;
> > >                 this.value = options.value;
> > >         },
> > >         myAlert:function() {
> > >                 alert(this.options.value + ' : ' + this.value);
> > >         }
> >
> > > });
> >
> > > var classObj1 = new myClass({value:1});
> > > classObj1.myAlert();
> > > var classObj2 = new myClass({value:2});
> > > classObj1.myAlert();
> >
> > I think you are better off to understand what is actually happening.
> > Javascript does not have classes - it isn't an object oriented
> > language, it's object based.  It uses prototypes for inheritance, not
> > classic OO inheritance.
> >
> > While you can paper over the differences, they are still there.  Two
> > features that often cause issues are the this keyword and closures,
> > which have combined here to cause an unexpected result.
> >
> > To write your "class" (it's actually a javascript constructor, but
> > whatever) using plain javascript, you would write something like:
> >
> > function MyClass(obj) {
> >   this.value = obj.value;}
> >
> > MyClass.prototype.options = {};
> > MyClass.prototype.showValue = function(){
> >   alert(this.options.value + ':' + this.value);
> >
> > }
> >
> > That's it.  Note that this takes less code than using Class.create().
> > Now if you test this:
> >
> > var x = new MyClass({value:1});
> > x.showValue();  // --> 1:1
> >
> > var y = new MyClass({value:2});
> > x.showValue();  // --> 2:1
> >
> > You get the result you didn't want - the options object is shared by
> > all instances of MyClass because it is a property of the constructor's
> > prototype.  If you want each instance of MyClass to have its own
> > options object, you add it in the constructor:
> >
> > function MyClass(obj) {
> >   this.value = obj.value;
> >   this.options = {};
> >   this.options.value = this.value;  // or obj.value;}
> >
> > MyClass.prototype.showValue = function(){
> >   alert(this.options.value + ':' + this.value);
> >
> > }
> >
> > Now when you create some instances:
> >
> > var x = new MyClass({value:1});
> > x.showValue();  // --> 1:1
> >
> > var y = new MyClass({value:2});
> > x.showValue();  // --> 1:1
> > y.showValue();  // --> 2:2
> >
> > Is that what you wanted?  To me, the above is much simpler and clearer
> > than using Class.create, and is less code.
> >
> > --
> > Rob
> >
>

Take a look at $super. http://prototypejs.org/api/class/create and the
example of subclassing the Animal into a Snake.



-- 
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Spinoffs" 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/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to