0

I have a function defined inside another function like so:

var bar;
function foo() {
    bar = function() {
        // This should output the full source code of foo()
    }
}

bar();

When I call bar(), it should output the full source code of foo(), the function in which it is defined, to the console. What code should go inside bar() to achieve this behavior?

After I have that part accomplished, how would I look further up the chain to see what function foo() was defined in (and so on and so forth), by only calling bar()?

Thanks!

Edit: please don't answer with unconstructive replies such as "you shouldn't be doing this" or "you'll never need this." If I didn't need it I wouldn't be asking.

Joey
  • 10,504
  • 16
  • 39
  • 54
  • If it didn't have a real world application, I wouldn't be doing it. You don't know everything. – Joey Apr 11 '14 at 18:56
  • 1
    Then read the comment again, if you need to get the calling functions as string, or the names of functions inside said functions etc. you're doing something wrong as you should never need that, and that's why there's no way to get it either. – adeneo Apr 11 '14 at 18:59
  • How do you know whether I do or don't need to get functions as strings? You have zero idea what my use case is. Gotta love the people who assume they know everything about what kind of code someone should or shouldn't need without knowing anything about their use case at all. – Joey Apr 11 '14 at 19:08
  • I'm not saying you don't need it, I'm saying that if you do you're probably doing something wrong. There's no reason you should have to do this, and that's why there's no way to do it, in fact some of the ways you could possibly do this is being removed in strict and newer versions of ecma because there should be no reason to access callee or get the name or string representation of a function in a higher scope, it's just bad practice. I'm just being nice trying to tell you that you should reconsider and try to find another way to do what you need to do, while you're being somewhat rude. – adeneo Apr 11 '14 at 19:45

1 Answers1

0

Are you looking for something along these lines? As long as you run bar() from foo(), this should work.

function foo() {
    self = foo;
    function bar() {
        console.log(self.toString());
    }
    bar();
}
foo();

You can test it out here: http://jsfiddle.net/eGwWG/

Adam
  • 4,445
  • 1
  • 31
  • 49
  • No because that is literally just hardcoding it. It should be able to get its parent's source without knowing the name before hand. Also, I'm going to call bar() by itself outside of all control structures. – Joey Apr 11 '14 at 18:50
  • I updated my fiddle, is that more along the lines of what you want? I'm unsure what your use-case is, so catering my code to what you need is kind of difficult. How do you plan on calling bar() outside of the scope of foo()? bar() is only defined in the scope of foo(), so hardcoding it seems appropriate. – Adam Apr 11 '14 at 18:54
  • No, at the end, I just want to call bar() by itself. I updated my post to make that more clear. Also, bar shouldn't have the name foo hardcoded into it, that defeats the whole idea and at that point I would just call foo.toString() outside the function. – Joey Apr 11 '14 at 19:00
  • Try out your code. You can't access bar() because you have to be within the scope of foo() to access it. Otherwise it's undefined. That is your fundamental flaw. You are trying to move up the scope, for no good reason. Give us some real world code in which you need to do this, and maybe we can help you more. http://jsfiddle.net/LJzSn/ – Adam Apr 11 '14 at 19:04
  • bar can be declared outside the scope, that shouldn't be a problem. – Joey Apr 11 '14 at 19:11
  • Judging from your code in the question, still wrong. foo() hasn't been executed, so `var bar` is undefined. Use jsfiddle.net to run some of this code before you post it. You *have* to be in the scope of foo() in order to run bar(), it is as simple as that. – Adam Apr 11 '14 at 19:15
  • This works: http://jsfiddle.net/HyYEp/ That's more or less how it will work in my use case. – Joey Apr 11 '14 at 19:18
  • Okay, so either way you have to enter the scope of foo(), so why not set a variable while you're in that scope? Something like [this](http://jsfiddle.net/HyYEp/1/). From my reading up on the topic, it looks like you can't just jump up to the containing function, because scope does not work that way in Javascript. You have to enter the scope of foo() at some point, in order to define bar(), so while you're in there, save the the function to a variable and use it later. – Adam Apr 11 '14 at 19:25