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 <[EMAIL PROTECTED]> 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@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---