function class1()
{
this.xxx = 42;
}
function class2()
{
this.yyy = 10;
}
class2.prototype = new class1;
var obj = new class2;
class2 extends class1. But what code could I put inside *class1* to tell me class2's name?
I mean, I'd like to be able to call obj.getClassName() or something but the code needs to be in the parent class1 since it's an abstract class extended by lots of different classes.
Parsing this.constructor.toString() gives me class1's name but I can't seem to get class2's name from in there unless I put the code inside class2 itself
Weirdan wrote:I doubt you'll be able to do that. Remember, there's no inheritance in JS, just prototype chain inspection.
Yeah that really sucks. I basically tried a few things playing about with information stored in 'prototype' but to no avail. I'll just have to keep with my current way of specifying the name manually via constructor parameter
How would you solve this problem in php, java, ... ? Why do you need this information?
I can't tell you a reason, but it feels wrong that there should be standard mechanism for a more general class' constructor to get the name of the more specialized class' object that is created.
volka wrote:How would you solve this problem in php, java, ... ? Why do you need this information?
I can't tell you a reason, but it feels wrong that there should be standard mechanism for a more general class' constructor to get the name of the more specialized class' object that is created.
It's not *needed*. It's for a unit testing framework to display the names of the test cases when they fail Like simpletest does. Not a big deal if the user has to specify manually.
function AbstractClassToExtend(childName)
{
this.name = childName || 'DefaultName'; //The name of the class which extends me
//Remaining methods here (for example)
this.x = 0;
this.setX = function(x)
{
this.x = x;
}
}
//Make a factory method (static) to create objects that extend me
AbstractClassToExtend.create = function(childClass)
{
var childClassName = childClass.toString().replace(/^\s*function\s+(\w+)\s*\((?:.|\s)*$/i, '$1'); //Get the name of the class BEFORE extending
childClass.prototype = new AbstractClassToExtend(childClassName);
return new childClass();
}
//Now make an extension for it
function myExtension()
{
this.getX = function()
{
return this.x;
}
}
//Use the factory to get the object
var myObject = AbstractClassToExtend.create(myExtension);
//Check we do actually have the right name
alert(myObject.name); //myExtension
//etc etc
myObject.setX(42);
alert(myObject.getX());
Tested with FF 1.5, Epiphany, Konqueror, IE 6, Galeon, Opera 8 and 9.