Herr Ernst
2008-Feb-06 08:51 UTC
Get class of object created via Prototype (Introspection)
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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Richard Quadling
2008-Feb-06 09:15 UTC
Re: Get class of object created via Prototype (Introspection)
You could cheat and simply add ... _className : ''Car''; to the class. On 06/02/2008, Herr Ernst <herr.ernst-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. > > > >-- ----- 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Tobie Langel
2008-Feb-06 09:39 UTC
Re: Get class of object created via Prototype (Introspection)
You can''t, that''s a limitation of the language, unfortunately. Tobie On Feb 6, 9:51 am, Herr Ernst <herr.er...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Herr Ernst
2008-Feb-06 10:33 UTC
Re: Get class of object created via Prototype (Introspection)
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 <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You can''t, that''s a limitation of the language, unfortunately. > > Tobie > > On Feb 6, 9:51 am, Herr Ernst <herr.er...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Tobie Langel
2008-Feb-06 10:40 UTC
Re: Get class of object created via Prototype (Introspection)
var benzclone = new window[benz._class]() Herr Ernst 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 <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > You can''t, that''s a limitation of the language, unfortunately. > > > > Tobie > > > > On Feb 6, 9:51 am, Herr Ernst <herr.er...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Tobie Langel
2008-Feb-06 11:00 UTC
Re: Get class of object created via Prototype (Introspection)
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 <herr.er...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > You can''t, that''s a limitation of the language, unfortunately. > > > Tobie > > > On Feb 6, 9:51 am, Herr Ernst <herr.er...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Herr Ernst
2008-Feb-06 12:28 UTC
Re: Get class of object created via Prototype (Introspection)
Thanks very much. Classes are not global, so I use the above method. Bye On Feb 6, 12:00 pm, Tobie Langel <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 <herr.er...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > You can''t, that''s a limitation of the language, unfortunately. > > > > Tobie > > > > On Feb 6, 9:51 am, Herr Ernst <herr.er...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 6, 8:33 pm, Herr Ernst <herr.er...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 7, 4:07 am, RobG <rg...-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote:> On Feb 6, 8:33 pm, Herr Ernst <herr.er...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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); > > -- > Robthis is an elaborate example of how you could implement something like this... var Car = Class.create({ initialize: function(make) { this.constructor = ''Car''; 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''); --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 7, 9:12 pm, Wizz <woutaw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Feb 7, 4:07 am, RobG <rg...-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote: > > On Feb 6, 8:33 pm, Herr Ernst <herr.er...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---