If all of your classes are globals only (i.e. no namespacing), you can
get away with something like this:
function getName(obj) {
for(var name in window)
if(window[name] === obj) return name;
}
getName(benz.constructor)
// -> "Car"
On Feb 6, 11:33 am, 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";
> }
>
> });
>
> var benz=new Car();
>
> //recreated benz
> var benzclone=eval("new "+benz._class+"()")
>
> Pretty dirty, isn't it. Or does someone know a better method?
>
> On Feb 6, 10:39 am, Tobie Langel <[EMAIL PROTECTED]> wrote:
>
> > You can't, that's a limitation of the language, unfortunately.
>
> > Tobie
>
> > On Feb 6, 9:51 am, Herr Ernst <[EMAIL PROTECTED]> wrote:
>
> > > Hi,
> > > I have a question.
> > > How is it possible in Prototype to determine the class of an object
> > > (i.e. constructor).
> > > Example:
> > > I define a class:
> > > "Car=Class.create({})"
> > > Then I instantiate it:
> > > "benz=new Car()"
>
> > > So and now I want to know what class benz is. I know I can do "benz
> > > instanceof Car" which returns true. But how can I get the class
> > > directly (I have many classes and don't want to compare every object
> > > with every class)?
>
> > > Thanks.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---