Re: Re: Prototype OOP example

Eric Fleming
Wed, 28 Dec 2005 20:20:06 -0000

Here is a great article on OOP Javascript.  It at least gives an
introduction.  Don''t let the title fool you, the article is almost all
about
OOP Javascript.  It was very helpful to me.

http://jdj.sys-con.com/read/152288.htm


On 12/28/05, Ryan Gahl <Ryan.Gahl-nlycWCgr5/vuufBYgWm87A@public.gmane.org>
wrote:
>
> Dear Original Poster,
>
> Try this...
>
> superClass = Class.create();
> superClass.prototype = { blah blah blah... }
>
> subclass = Class.create();
> subclass.prototype = {
>         initialize: function(name, position) {
>                 Object.extend(this, new superClass(name));
>                 this.position = position;
>         }
> }
>
> -----Original Message-----
> From:
rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
>
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]
On Behalf Of Cliff
> Mees
> Sent: Monday, December 26, 2005 9:20 PM
> To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> Subject: Re: [Rails-spinoffs] Re: Prototype OOP example
>
> Victor,
>
> Thanks for putting that clearer (and more correctly) than I did!
>
> Object#extend does return the extended object, but also affects the
> first parameter ("destination") in-place, extending it without
> requiring the return.
>
> My initial example actually re-defined the base class in terms of the
> inherited class (i.e. Labour and Worker had the same definitions --
> oops!).
>
> I take it that the return from Object#extend is provided to allow
> things like:
>
> Object.extend(base_obj, {newMethod: function() { // do
> something }}).newMethod();
>
>
> Cheers,
>
> Cliff
>
> On Dec 26, 2005, at 15:56, Victor Borja wrote:
>
> > QnA wrote:
> >> Thank you for your example. It is a bit disappointed to me that it
> >> is not
> >> possible to call the superclass''s constructor in
prototype.
> >
> > There''s no such thing as a REAL class in JavaScript (in the
sense of
> > classes as in java, ruby, etc), you have objects (wich have
> > properties)
> > and functions (wich are objects).
> >
> > In JS you get only _Initializer Functions_, and fake class
> > inheritance by
> > calling another initializer within your _subclass_.
> > Checkout http://javascript-reference.info/#oop
> >
> > /* Labour is just a _Constructor Function_.
> >    Prototype''s Class.create just returns a new function that
calls
> >    this.initialize to follow the ruby OO tradition */
> > Labour = Class.create();
> > Labour.prototype = {
> >     initialize: function(name) {
> >         this.name = name;
> >     },
> >
> >     showYourName: function() {
> >         alert(this.name);
> >     }
> > }
> >
> > /* Worker will be a subclass of Labour */
> > Worker = Class.create();
> > /* Thanks to Prototype''s Object.extend you can copy all
properties
> > from
> >    an object to another, we are using this to get all the Labour
> >    functions in Worker, just as if we had inheritance */
> > Object.extend(Worker.prototype, Labour.prototype);
> > /* Next extend our _subclass_, note that we are not using something
> > like:
> >       Worker.prototype = {  // bla bla }
> >    Because we would be throwing away all the Labour.prototype
> > properties,
> >    Instead we just use Object.extend again.
> >  */
> > Object.extend(Worker.prototype, {
> >
> >     /* define our _subclass_ initializer, this overrides the old
> >        Labour#initialize.
> >        We could as well, backup the old initializer. I named it
> > _super_
> >        but it could be whatever you like.
> >     */
> >     super: Labour.prototype.initialize,
> >
> >     initialize: function(name, position) {
> >       // Calling the _superclass_ initializer
> >       super.call(this, name);
> >       this.position = position;
> >     },
> >
> >     showYourPosition: function() {
> >       alert(this.position);
> >     },
> >
> > });
> >
> > Regards,
> > --
> > vic
> >
> > _______________________________________________
> > Rails-spinoffs mailing list
> > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
>
> _______________________________________________
> Rails-spinoffs mailing list
> Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
>
> The information transmitted in this electronic mail is intended only for
> the
> person or entity to which it is addressed and may contain confidential,
> proprietary, and/or privileged material.  Any review, retransmission,
> dissemination or other use of, or taking of any action in reliance upon,
> this information by persons or entities other than the intended recipient
> is prohibited. If you received this in error, please contact the sender
> and
> delete the material from all computers.
>
> _______________________________________________
> Rails-spinoffs mailing list
> Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
>



--
Eric Fleming
efleming-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org