Yes or no, depending on what you mean.

You can access the `MyCoolClass` variable inside your code, of course,
as it's defined in the scope that contains the functions you've passed
into `Class.create`. But I'm thinking that's not what you mean.

You can't get the string "MyCoolClass", if that's what you mean. You'd
have to build that into your own code, for instance like this:

var MyCoolClass = Class.create({
  className: "MyCoolClass",

  initialize: function() {

...but unless you want an actual string for debugging output or
something, there's no need to get the *name* of the "class" (which is
to say, the name of the variable you assigned the constructor function
to). You can access that function directly via `this.constructor` if
you need access to it. In your case, for instance:

    initialize: function() {
        alert(this.constructor === MyCoolClass); // alerts true

This can be handy for writing fairly generic code. For instance,
suppose you want a function that accepts an argument and wants (for
whatever reason) to see if that argument is the same "type" as the
instance itself:

    foo: function(x) {
        if (x instanceof this.constructor) {
            // Yes it is

...or for a cloning routine:
* * * *
  var MyCoolClass = Class.create({
    initialize: function() {
    clone: function() {
      var c, name;

      c = new this.constructor();
      for (name in this) {
        if (this.hasOwnProperty(name)) {
          c[name] = this[name];

      return c;

  var m = new MyCoolClass();
  m.x = "foo";
  display("m instanceof MyCoolClass? " + (m instanceof MyCoolClass));
  display("m.x = " + m.x);

  var c = m.clone();
  display("c instanceof MyCoolClass? " + (m instanceof MyCoolClass));
  display("c.x = " + c.x);
* * * *

T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com

On Dec 16, 4:56 pm, Luke <kickingje...@gmail.com> wrote:
> Hi,
> If you define a class like
> var MyCoolClass = Class.create({
>   initialize: function() {
>     // find out classname here
>   }
> });
> can you get the name of the class from within itself? After all it's
> just a variable-name, but is it somehow accessible?
> Thank you
> Lukas

You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
For more options, visit this group at 

Reply via email to