8
function MyFunction() {
  closure = function() {
    alert('my parent function name is: '/* now what here ? */);
  }
  closure();
}
MyFunction();

Result should be my parent function name is: MyFunction

(To moderators: I do not know why stackoverflow is preventing me from sending this question claiming it doesn't meet quality standards. Do I must type some superfluous text to be allowed to send this.)

rsk82
  • 28,217
  • 50
  • 150
  • 240
  • 3
    Just typing some code without including a question is definitely not up to SO standards, no. I see you put your question into the subject line, but that's not enough. – Ernest Friedman-Hill Aug 16 '11 at 12:32
  • take a look at this http://stackoverflow.com/questions/2648293/javascript-get-function-name – Liviu T. Aug 16 '11 at 12:35
  • 1
    http://stackoverflow.com/questions/280389/how-do-you-find-out-the-caller-function-in-javascript maybe dublicate – Nastya Kholodova Aug 16 '11 at 12:35
  • And another : [Can I get the name of the currently running function in javascript?](http://stackoverflow.com/questions/1013239/can-i-get-the-name-of-the-currently-running-function-in-javascript) – My Head Hurts Aug 16 '11 at 12:38
  • It's worth noting that you can also just do `MyFunction.name`. – skeggse Jun 30 '15 at 04:44

3 Answers3

3

That is/was possible, but it is restricted. First restriction, not all Javascript engines support the following pattern and second (more dramatic), ES5 strict mode does not support it either.

Some Javascript engines allow you to access the .__parent__ property which is a reference to the parent scope. That should look like

alert('my parent is ' + this.__parent__.name );

where you would have to call new closure(), or give the function a name and use that name instead of this.

However, most implementations removed that access for security concerns. As you may noticed, you're able to breach the 'allmighty' closure security by beeing able to access the parent context.


Another way to accomplish it, is to access arguments.callee.caller, which also is not accessible in ES5 strict mode. Looks like:

function MyFunction() {
  closure = function f() {
    alert('my parent function name is: ' +  arguments.callee.caller.name);
  }
 closure();
}
MyFunction();
jAndy
  • 231,737
  • 57
  • 305
  • 359
2
function MyFunction() {
  closure = function() {
    alert('my parent function name is: ' + arguments.callee.caller.name);
  }
  closure();
}
MyFunction();
mVChr
  • 49,587
  • 11
  • 107
  • 104
1
function MyFunction() {
    var functionName = arguments.callee.name,
    closure = function() {
        alert('my parent function name is: ' + functionName);
    }
    closure();
}
MyFunction();
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • After thinking more about it: This gives you the name of the function where the closure is *defined* in, not where it is *run* (though in this special case it does). – Felix Kling Aug 16 '11 at 12:45
  • He's looking for the closure, not the parent object. That you can get with `this`. You might also be interested in `arguments.caller` and `Function.caller`: https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope/arguments/caller – Joseph Silber Aug 16 '11 at 12:47
  • The closure is defined by where the function is *defined*, **not** by where it is *run*! – Joseph Silber Aug 16 '11 at 12:51
  • Yes, I know. The question was *"is it possible to get function name in which given closure is **running**"*. Your code only works if the closure is run in the same function it was defined. That's all I wanted to say. – Felix Kling Aug 16 '11 at 12:53
  • In that case, I don't even understand the question. What does it mean *in which given closure is running*? That is what the `this` keyword is used for! What am I missing? – Joseph Silber Aug 16 '11 at 12:57
  • Imagine a function that returns a closure. Now I have `function A() { var c = getClosure(); c();}; A();`. How would the closure get the name `A` here? (btw. the other answers show how). `this` most likely refers to `window` if the function is not called as `object` method. – Felix Kling Aug 16 '11 at 13:02