Say I wanted to get the name of the instance of a object/prototype class, what''s the best method of doing so? Is there some function/property in prototype to do this that I''ve missed? Btw I dont mean just refering to the instance (this) I want the actual name of the instance (nameofinstance). var nameofinstance = Class.create({ initialize: function() { alert(...nameofinstance...); } }); --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi, In the example you gave, there is no way for the initialize() method to know that the class is referenced by a variable called "nameofinstance" (shouldn''t that be "nameofclass"?); "nameofinstance" is completely external to the "class" (actually a constructor function), it''s just a variable that references it. Consider this code: * * * * var nameofinstance; var someothervar; nameofinstance = Class.create({ initialize: function() { alert(...nameofinstance...); } }); someothervar = nameofinstance; * * * * Now what would initialize() say? Both variables reference the same "class". Some frameworks that provide class-like stuff handle this by having you pass in a name you''re giving the class to their equivalent of Class.create(), and then they make that available via a getClassName() method or similar. I don''t think Prototype provides that with its Class stuff (and I''m not at all sure it should), but you can certainly do so with your own classes: * * * * var nameofinstance; nameofinstance = Class.create({ initialize: function() { alert(...nameofinstance...); }, getClassName: function() { return "NameOfClass"; } }); * * * * Hope this helps, -- T.J. Crowder tj / crowder software / com On Mar 27, 7:40 am, Jonas Rosenqvist <jon...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Say I wanted to get the name of the instance of a object/prototype > class, what''s the best method of doing so? > > Is there some function/property in prototype to do this that I''ve > missed? > > Btw I dont mean just refering to the instance (this) I want the actual > name of the instance (nameofinstance). > > var nameofinstance = Class.create({ > initialize: function() { > alert(...nameofinstance...); > } > > });--~--~---------~--~----~------------~-------~--~----~ 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 Mar 27, 5:40 pm, Jonas Rosenqvist <jon...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Say I wanted to get the name of the instance of a object/prototype > class, what''s the best method of doing so? > > Is there some function/property in prototype to do this that I''ve > missed? > > Btw I dont mean just refering to the instance (this) I want the actual > name of the instance (nameofinstance).Objects can have a property called name that has a value. Any number of variables can be assigned a reference to the same object, which one do you expect to represent the name property? e.g.: var a = {name: ''foo''}; var b = a; var c = b; a, b and c all reference the same object. -- 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 -~----------~----~----~----~------~----~------~--~---
Jonas Rosenqvist
2008-Mar-27 10:14 UTC
Re: Getting name of object instance from within object
Thanks for your replys guys, I see both of your points but I still need to get the name of the instance of the object, not a reference. The reason for this beeing that I''m creating dom elements that has to be able to refer back to the the instance of the class whom created them. And there may or may not be many instances of the same class so it cant be done in any static way or with references as the dom objects have a different scope. The way I''ve solved it for the time beeing is passing the name of the instance upon creation as a argument of class initialization. That works great but it just looks stupid to me. A function or property refering directly to the name/objectindex of the obejct/class instance would be much more practical. There''s functions for this but it''s mozilla only (I think it''s called object.constructor.name or something), and there''s ways to do it in IE and possibly other browsers too but they''re ugly. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi,> A function or property refering directly to the name/objectindex of > the obejct/class instance would be much more practical.But, again, what you''re looking for, as you''ve stated it, is impossible. An instance can be referred to by any number of variables, and the instance can have no idea what the names of those variables are. But based on your later comments, I don''t think you''re looking for the name of the variable pointing to an instance anyway. You''re looking for the name of the constructor function (which people sometimes call a "class"). E.g., in the non-Prototype world: * * * * function MyNiftyThing() { // .... } MyNiftyThing.prototype.makeSomeElements = funtion() { // make some elements } * * * * You want to know that the elements were created by MyNiftyThing. And indeed, in the non-Prototype world, Mozilla''s constructor.name property would tell you that, but as far as I know that''s not supported by anyone else and even if it were, the way Class.create works, Mozilla''s custom property would always return "klass" anyway. Here''s how to do what I think you want to do, both non-Prototype and Prototype (tested on FF, IE6, Opera9, Safari 3 Windows beta): * * * * // Non-Prototype function MyNiftyThing() { // Remember the name of the constructor function using a custom // ''ctorName'' property arguments.callee.ctorName = ''MyNiftyThing''; } MyNiftyThing.prototype.getCtorName = function() { return this.constructor.ctorName; } // Prototype var MyPrototypeThing = Class.create({ initialize: function() { this.constructor.theClass = ''MyPrototypeThing''; }, getCtorName: function() { return this.constructor.ctorName; } }); * * * * Strictly speaking you don''t need the getCtorName function, anything with access to one of the instance of the class can see the name directly: * * * * var t; t = new MyPrototypeThing(); alert("It''s constructor name is " + t.constructor.ctorName); * * * * But it''s better to encapsulate that using a function. All of this is possible because "classes" are just constructor functions, and functions are objects just like anything else, and so can have additional properties. In this case, we''re adding a ctorName property to the constructor function that we can use later. When you make the elements, you will still have to give them something they can use to find out who created them -- e.g., a reference to the instance that created them, or just the name of the constructor function [by giving them the return value from this.getCtorName()], something like that.. Hope this helps, -- T.J. Crowder tj / crowder software / com On Mar 27, 10:14 am, Jonas Rosenqvist <jon...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks for your replys guys, I see both of your points but I still > need to get the name of the instance of the object, not a reference. > > The reason for this beeing that I''m creating dom elements that has to > be able to refer back to the the instance of the class whom created > them. And there may or may not be many instances of the same class so > it cant be done in any static way or with references as the dom > objects have a different scope. > > The way I''ve solved it for the time beeing is passing the name of the > instance upon creation as a argument of class initialization. That > works great but it just looks stupid to me. > > A function or property refering directly to the name/objectindex of > the obejct/class instance would be much more practical. There''s > functions for this but it''s mozilla only (I think it''s called > object.constructor.name or something), and there''s ways to do it in IE > and possibly other browsers too but they''re ugly.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Sorry, there''s a copy-and-paste error in the above. The line in the Prototype solution''s initialize() function that reads> this.constructor.theClass = ''MyPrototypeThing'';should read> this.constructor.ctorName = ''MyPrototypeThing'';Sorry ''bout that. -- T.J. Crowder tj / crowder software / com On Mar 28, 11:43 am, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > > A function or property refering directly to the name/objectindex of > > the obejct/class instance would be much more practical. > > But, again, what you''re looking for, as you''ve stated it, is > impossible. An instance can be referred to by any number of > variables, and the instance can have no idea what the names of those > variables are. > > But based on your later comments, I don''t think you''re looking for the > name of the variable pointing to an instance anyway. You''re looking > for the name of the constructor function (which people sometimes call > a "class"). E.g., in the non-Prototype world: > > * * * * > function MyNiftyThing() > { > // ....} > > MyNiftyThing.prototype.makeSomeElements = funtion() > { > // make some elements} > > * * * * > > You want to know that the elements were created by MyNiftyThing. And > indeed, in the non-Prototype world, Mozilla''s constructor.name > property would tell you that, but as far as I know that''s not > supported by anyone else and even if it were, the way Class.create > works, Mozilla''s custom property would always return "klass" anyway. > > Here''s how to do what I think you want to do, both non-Prototype and > Prototype (tested on FF, IE6, Opera9, Safari 3 Windows beta): > > * * * * > // Non-Prototype > function MyNiftyThing() > { > // Remember the name of the constructor function using a custom > // ''ctorName'' property > arguments.callee.ctorName = ''MyNiftyThing'';} > > MyNiftyThing.prototype.getCtorName = function() > { > return this.constructor.ctorName; > > } > > // Prototype > var MyPrototypeThing = Class.create({ > initialize: function() { > this.constructor.theClass = ''MyPrototypeThing''; > }, > getCtorName: function() > { > return this.constructor.ctorName; > }}); > > * * * * > > Strictly speaking you don''t need the getCtorName function, anything > with access to one of the instance of the class can see the name > directly: > * * * * > var t; > t = new MyPrototypeThing(); > alert("It''s constructor name is " + t.constructor.ctorName); > * * * * > > But it''s better to encapsulate that using a function. > > All of this is possible because "classes" are just constructor > functions, and functions are objects just like anything else, and so > can have additional properties. In this case, we''re adding a ctorName > property to the constructor function that we can use later. > > When you make the elements, you will still have to give them something > they can use to find out who created them -- e.g., a reference to the > instance that created them, or just the name of the constructor > function [by giving them the return value from this.getCtorName()], > something like that.. > > Hope this helps, > -- > T.J. Crowder > tj / crowder software / com > > On Mar 27, 10:14 am, Jonas Rosenqvist <jon...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Thanks for your replys guys, I see both of your points but I still > > need to get the name of the instance of the object, not a reference. > > > The reason for this beeing that I''m creating dom elements that has to > > be able to refer back to the the instance of the class whom created > > them. And there may or may not be many instances of the same class so > > it cant be done in any static way or with references as the dom > > objects have a different scope. > > > The way I''ve solved it for the time beeing is passing the name of the > > instance upon creation as a argument of class initialization. That > > works great but it just looks stupid to me. > > > A function or property refering directly to the name/objectindex of > > the obejct/class instance would be much more practical. There''s > > functions for this but it''s mozilla only (I think it''s called > > object.constructor.name or something), and there''s ways to do it in IE > > and possibly other browsers too but they''re ugly.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jonas Rosenqvist
2008-Apr-01 07:14 UTC
Re: Getting name of object instance from within object
On 28 Mar, 13:43, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > When you make the elements, you will still have to give them something > they can use to find out who created them -- e.g., a reference to the > instance that created them, or just the name of the constructor > function [by giving them the return value from this.getCtorName()], > something like that..Let me begin by saying thanks for your great reply :) And you''re exactly right, when I create the elements I either have to give them a reference to the instance (which might be done using addMethod) or . The thing is I''ll be creating lots of them and the only way to do this in any rational matter is by using onclick, with addmethod it might be done using this.boundmethodname() and then creating a special method for each instance of the controlling DOM element. But it would be more elegant and "cheaper" to just refer to the class instance as onclick="instancename.instancemethod()". Just refering to the constructor function wont work since I wont be creating a new class instance upon the event firing from a static element, but rather creating the dom element from a static instance of the class and interacting with it from the created element. Anyways, just passing the instance name as an argument works fine and what I''m looking for just seems to be one of those things javascript just aint built for. --~--~---------~--~----~------------~-------~--~----~ 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 Mar 28, 9:43 pm, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > A function or property refering directly to the name/objectindex of > > the obejct/class instance would be much more practical.That infers that you are going to execute logic based on the "name" of the constructor. One of the objectives of OO programming is that you don''t care what the constructor was, you test if the object has the method you want to use (or implement logic so that you know that the method exists) and then call it, the method should already be set to the appropriate function for that object. [...]> But based on your later comments, I don''t think you''re looking for the > name of the variable pointing to an instance anyway. You''re looking > for the name of the constructor function (which people sometimes call > a "class"). E.g., in the non-Prototype world: > > function MyNiftyThing() > { > // ....} > > MyNiftyThing.prototype.makeSomeElements = funtion() > { > // make some elements} > > You want to know that the elements were created by MyNiftyThing. And > indeed, in the non-Prototype world, Mozilla''s constructor.name > property would tell you that, but as far as I know that''s not > supported by anyone else and even if it were, the way Class.create > works, Mozilla''s custom property would always return "klass" anyway.The constructor.name property is easily implemented (as you do below).> Here''s how to do what I think you want to do, both non-Prototype and > Prototype (tested on FF, IE6, Opera9, Safari 3 Windows beta): > > * * * * > // Non-Prototype > function MyNiftyThing() > { > // Remember the name of the constructor function using a custom > // ''ctorName'' property > arguments.callee.ctorName = ''MyNiftyThing'';}It doesn''t seem sensible to set the constructor''s name property every time it is called. It makes more sense to set it once on the constructor''s prototype. :-)> > MyNiftyThing.prototype.getCtorName = function() > { > return this.constructor.ctorName; > > } > > // Prototype > var MyPrototypeThing = Class.create({ > initialize: function() { > this.constructor.theClass = ''MyPrototypeThing''; > }, > getCtorName: function() > { > return this.constructor.ctorName; > }}); > > * * * * > > Strictly speaking you don''t need the getCtorName function, anything > with access to one of the instance of the class can see the name > directly: > * * * * > var t; > t = new MyPrototypeThing(); > alert("It''s constructor name is " + t.constructor.ctorName); > * * * * > > But it''s better to encapsulate that using a function.Why? It doesn''t offer anything that isn''t provided by: MyNiftyThing.prototype.ctorName = ''whatever''; They are both public properties that are easily overridden. One use for a function is to create the ctorName property as a private member, then the get function can be a privileged function: <URL: http://www.crockford.com/javascript/private.html > -- 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 -~----------~----~----~----~------~----~------~--~---
> It doesn''t seem sensible to set the constructor''s name property every > time it is called. It makes more sense to set it once on the > constructor''s prototype. :-)You''re quite right. :-) It was a quick exmaple for the OP.> > Strictly speaking you don''t need the getCtorName function > > ... > > But it''s better to encapsulate that using a function. > > Why? It doesn''t offer anything that isn''t provided by: > > MyNiftyThing.prototype.ctorName = ''whatever'';Again, this wasn''t a rigorously-designed thing. But I was encapsulating retrieval, not assignment. It''s worthwhile because I may want to change how I store the name down-the-line. Yes, all properties are public unless you play closure games a''la Crockford, which I might do if it were important to me to hide the data thoroughly; in this case it wasn''t. ;-) -- T.J. Crowder tj / crowder software / com On Apr 1, 9:47 am, RobG <rg...-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote:> On Mar 28, 9:43 pm, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi, > > > A function or property refering directly to the name/objectindex of > > > the obejct/class instance would be much more practical. > > That infers that you are going to execute logic based on the "name" of > the constructor. One of the objectives of OO programming is that you > don''t care what the constructor was, you test if the object has the > method you want to use (or implement logic so that you know that the > method exists) and then call it, the method should already be set to > the appropriate function for that object. > > [...] > > > > > But based on your later comments, I don''t think you''re looking for the > > name of the variable pointing to an instance anyway. You''re looking > > for the name of the constructor function (which people sometimes call > > a "class"). E.g., in the non-Prototype world: > > > function MyNiftyThing() > > { > > // ....} > > > MyNiftyThing.prototype.makeSomeElements = funtion() > > { > > // make some elements} > > > You want to know that the elements were created by MyNiftyThing. And > > indeed, in the non-Prototype world, Mozilla''s constructor.name > > property would tell you that, but as far as I know that''s not > > supported by anyone else and even if it were, the way Class.create > > works, Mozilla''s custom property would always return "klass" anyway. > > The constructor.name property is easily implemented (as you do below). > > > Here''s how to do what I think you want to do, both non-Prototype and > > Prototype (tested on FF, IE6, Opera9, Safari 3 Windows beta): > > > * * * * > > // Non-Prototype > > function MyNiftyThing() > > { > > // Remember the name of the constructor function using a custom > > // ''ctorName'' property > > arguments.callee.ctorName = ''MyNiftyThing'';} > > It doesn''t seem sensible to set the constructor''s name property every > time it is called. It makes more sense to set it once on the > constructor''s prototype. :-) > > > > > > > MyNiftyThing.prototype.getCtorName = function() > > { > > return this.constructor.ctorName; > > > } > > > // Prototype > > var MyPrototypeThing = Class.create({ > > initialize: function() { > > this.constructor.theClass = ''MyPrototypeThing''; > > }, > > getCtorName: function() > > { > > return this.constructor.ctorName; > > }}); > > > * * * * > > > Strictly speaking you don''t need the getCtorName function, anything > > with access to one of the instance of the class can see the name > > directly: > > * * * * > > var t; > > t = new MyPrototypeThing(); > > alert("It''s constructor name is " + t.constructor.ctorName); > > * * * * > > > But it''s better to encapsulate that using a function. > > Why? It doesn''t offer anything that isn''t provided by: > > MyNiftyThing.prototype.ctorName = ''whatever''; > > They are both public properties that are easily overridden. One use > for a function is to create the ctorName property as a private member, > then the get function can be a privileged function: > > <URL:http://www.crockford.com/javascript/private.html> > > -- > 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 -~----------~----~----~----~------~----~------~--~---