On Feb 6, 8:33 pm, Herr Ernst <[EMAIL PROTECTED]> wrote:
> This is bad news for me.
> Maybe you can help me though. My users can create some classes in a
> particular order which are stored at runtime in an array. But I want
> to make this persistent, i.e. storing in a cookie. I thought about
> building a serialized string which contains the class/constructor and
> the constructor arguments for each object. I've helped myself with the
> following hack:
>
> 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

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