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

Reply via email to