On Feb 7, 9:12 pm, Wizz <[EMAIL PROTECTED]> wrote:
> On Feb 7, 4:07 am, RobG <[EMAIL PROTECTED]> wrote:
> > On Feb 6, 8:33 pm, Herr Ernst <[EMAIL PROTECTED]> wrote:
[...]
> > > Car=Class.create({
> > >   initialize: function() {
> > >     this._class="Car";
> > >   }
>
> > It would be better to reference the object itself rather than it's
> > name:
>
> >       this._class = Car;


>
> > As far as I know, using an underscore for a property name is intended
> > to indicate a private variable, but _class is public and "class" is a
> > reserved word.  Why not just use the public constructor property?
>
> > > });
>
> > > var benz=new Car();
>
> > > //recreated benz
> > > var benzclone=eval("new "+benz._class+"()")
>
> >   var benzclone = new benz.constructor();
>
> > > Pretty dirty, isn't it. Or does someone know a better method?
>
> > See above.  Alternatively, consider using the features built into the
> > language.  Use a normal constructor and the constructor property of
> > objects constructed from it:
>
> >   function Car(make) {
> >     this.make = make;
> >   }
>
> >   var benz = new Car('Benz');
> >   var fiat = new benz.constructor('Fiat');
>
> >   alert(fiat.make);
>
> > --
> > Rob
>
> this is an elaborate example of how you could implement something like
> this...

I think you meant to reply to the OP.

> var Car = Class.create({
>   initialize: function(make) {
>     this.constructor = 'Car';

See above.


>     this.make = make;
>   }
>
> });
>
> var Fiat = Class.create(Car, {
>
>   initialize: function($super, type) {
>     this.constructor = 'Fiat';
>     $super('Fiat');
>     this.type = type;
>   }
>
> });
>
> var panda = new Fiat('Panda');

The question (if I understood it correctly) was how to create an
object from the same constructor as some other object when you don't
know what the original constructor was - i.e. something like:

function makeAnother( obj, args ) {

  return another = new obj.constructor( args );

}


--
Rob

--~--~---------~--~----~------------~-------~--~----~
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