Hi, when using prototype is would like to be able to get the class-
name of an instance of class. As prototype Class if defined now, it''s
not possible.
// Example
var X = Class.create({});
var x = new X();
x.constructor.name // is "klass" not "X"
I''ve made a modification to Class to fix this, I''ve added an
optional
parameter to create, so that if the first argument is a string, then
that is the class-name. Currently it also defines the class in the
global scope top.
// Example
Class.create("X", {});
var x = new X();
x.constructor.name // is "X"
Appreciate any comments or alternative solutions.
Best regards,
Tobias
var Class = {
create: function() {
var klass = function () { this.initialize.apply(this,
arguments); }
var parent = null, properties = $A(arguments);
// Creating class with correct name
if(Object.isString(properties[0])) {
var class_name = properties.shift();
klass = eval("function " + class_name +
" () { this.initialize.apply(this, arguments); };"
+
class_name + ";");
top[class_name] = klass;
}
if (Object.isFunction(properties[0]))
parent = properties.shift();
Object.extend(klass, Class.Methods);
klass.superclass = parent;
klass.subclasses = [];
if (parent) {
var subclass = function() { };
subclass.prototype = parent.prototype;
klass.prototype = new subclass;
if(parent.subclasses) parent.subclasses.push(klass);
klass.prototype.super = {};
}
for (var i = 0; i < properties.length; i++)
klass.addMethods(properties[i]);
if (!klass.prototype.initialize)
klass.prototype.initialize = function() {};
klass.prototype.constructor = klass;
return klass;
}
};
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---
On 12 Feb, 13:49, Tobias Nurmiranta <nurmira...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, when using prototype is would like to be able to get the class- > name of an instance of class. As prototype Class if defined now, it''s > not possible. > > // Example > var X = Class.create({}); > var x = new X(); > x.constructor.name // is "klass" not "X"Makes sense.> I''ve made a modification to Class to fix this, I''ve added an optional > parameter to create, so that if the first argument is a string, then > that is the class-name. Currently it also defines the class in the > global scope top.If you''re using the top I think you''re using then it''s not such a great idea.>.....> top[class_name] = klass;Well, it seems you''re actually doing that. The problem here is that top refers to the top frame in the frame hierarchy. Not the top of the javascript scope in the current frame (if you can call it that). If you change to window[class_name] = klass; then I''ll agree. Or do you really mean to refer to the top frame? /Marcus --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---
should be wrapped correctly this time:
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
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---