VK said:
Quick'N'Durty (RegExp, constructor chain and so are welcome):
<script type="text/javascript">
function FunctionOne() {
FunctionTwo();
}
function FunctionTwo() {
var s = '';
if (arguments.callee) {
var thisName = getObjectName(arguments.callee);
s+= "My name is " + thisName;
}
if (arguments.caller) {
s+= "\nI'm called by ";
s+= getObjectName(eval(thisName+".caller"));
}
alert(s);
}
function getObjectName(obj) {
var tmp = obj.toString();
return tmp.substring(tmp.indexOf(' ')+1,tmp.indexOf('('));
}
</script>
You're trying to get the object's constructor's name (coffee, in this
case), (and doing it badly, besides), but I gather what the OP wants is
the name of the variable containing the reference to the object (milk,
in this case).
Part of the problem is I'm pretty sure is that milk doesn't yet exist
at the time that coffee is called.
In 'milk = new coffee()', I'm *fairly* sure that coffee() is called
first, then = works its magic to autovivify it if necessary, et cetera,
then actually does the assignment.
All of that is better explained (or debunked entirely) by a real expert
on the language.
One way to workaround might be to say
milk = new coffee( 'milk' );
but that's not really what the OP wants.
The other problem is here:
milk = new coffee( 'milk' );
sugar = milk;
sugar and milk now reference to the same object, but the object thinks
it's milk. It doesn't know about sugar.
milk = new coffee( 'milk' );
sugar = milk;
milk = new coffee( 'milk' );
and now two objects think they are milk, when one of them is really
sugar. sugar thinks it's milk, and if it tries to operate on what it
thinks itself really is, it'll actually be operating on milk.
I don't believe that it's possible (or possibly adviseable) to do what
the OP is after, but maybe if we understood why he wanted to do it, we
could suggest another way to achieve the ultimate end goal.