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:

http://groups.google.com/group/rubyonrails-spinoffs/browse_thread/thread/b712bb66a1866f74

which discusses an approach to achieve this and seems backward
compatible.

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

---

Usage:

<script>
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();

alert(v.getClassName());
alert(c.getClassName());
alert(p.getClassName());
</script>

---

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;
        klass.prototype.constructor.addMethods({
                getClassName: function() {
                        return this.constructor.className;
                }
        });

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

    for (var i = 0; i < properties.length; i++)
      klass.addMethods(properties[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 prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to