As prototype provides a convenient OO approach to javascript coding
through its object inheritance model it would be 'really' useful if
you could query the name of the current class through a getClassName()
method or similar like you can in many other languages.

There is a post at:

which discusses an approach to achieve this and seems backward

Code and usage below. Any chance of including 'something' like this in
a future release?



Class.create("Vehicle", { });
Class.create("Car", Vehicle, { });    // Car extends Vehicle
Class.create("Passat", Car, { });     // Passat extends Car

// create one of each
var v = new Vehicle();
var c = new Car();
var p = new Passat();



Class rewrite:

var Class = {
  create: function() {

    var parent = null, properties = $A(arguments);

    // retrieve the class name
    var className = "";
    if(Object.isString(properties[0])) {
                className = properties.shift();

        // get the parent
    if (Object.isFunction(properties[0]))
      parent = properties.shift();

    function klass() {
                this.initialize.apply(this, arguments);

    Object.extend(klass, Class.Methods);
    klass.superclass = parent;
    klass.subclasses = [];

    // reference the class by name
        if(className!="") {
                window[className] = klass;

    // store the classname as klass.constructor.className
    // and add a method getClassName() to all classes
    klass.className = className;
                getClassName: function() {
                        return this.constructor.className;

    if (parent) {
      var subclass = function() { };
      subclass.prototype = parent.prototype;
      klass.prototype = new subclass;

    for (var i = 0; i < properties.length; i++)

    if (!klass.prototype.initialize)
      klass.prototype.initialize = Prototype.emptyFunction;

    klass.prototype.constructor = klass;

    return klass;
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at

Reply via email to