views:

142

answers:

2

How can I access a function name from inside that function?

// parasitic inheritance
var ns.parent.child = function() {
  var parent = new ns.parent();
  parent.newFunc = function() {

  }
  return parent;
}

var ns.parent = function() {
  // at this point, i want to know who the child is that called the parent
  // ie

}

var obj = new ns.parent.child();
A: 

You can't. Functions don't have names according to the standard (though mozilla has such an attribute) - they can only be assigned to variables with names.

Also your comment:

// access fully qualified name (ie "my.namespace.myFunc")

is inside the function my.namespace.myFunc.getFn

What you can do is return the constructor of an object created by new

So you could say

var obj = new my.namespace.myFunc();
console.info(obj.constructor); //my.namespace.myFunc
plodder
i need it inside the func because i'm passing it up the inheritance chain and I omitted that stuff for brevity.
Scott
I've edited the question to be more complete.
Scott
'this' will be the instance that invoked the function. So if you 'this' inside the parent function that will give you the instance of the function that called it. That's all you should need - not sure why you need the name too
plodder
well, in the parent, i can then access other functions by convention, such as ns[child][schema] or ns[child][dbService]. Without it, I have to hard code these references in every child class.
Scott
A: 

look here: http://www.tek-tips.com/viewthread.cfm?qid=1209619

arguments.callee.toString();

seems to be right for your needs.

Manuel Bitto
nope. see edit above.
Scott
arguments.callee.toString() just returns the source of the function.
Scott