Hi there,
got rid of the eval and added a getClassName method. Seems to work for
what I need to do:
----
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;
}
};
----
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>
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
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
-~----------~----~----~----~------~----~------~--~---