-1

I try to give a abroad picture from my question. Suppose I have a function, inside this function I have some other function calls and finally, first function returns a value.

function a() {
  return 1;
}

function b() {
  var result = a();
  return 2 + result;
}

b();

Now, after calling b() method, I want to know how many and what functions called inside the b() function. How can I achieve it?

Afshin Mehrabani
  • 33,262
  • 29
  • 136
  • 201
  • 1
    http://stackoverflow.com/questions/10182387/any-tools-to-draw-call-graphs-for-given-javascript might give you some glues. – Wolfgang Kuehn Jun 29 '13 at 14:25
  • 2
    Do you want to know it for debugging or to create program logic with it? The latter would be a bad design decision, as it would violate the [concept of encapsulation](http://en.wikipedia.org/wiki/Separation_of_concerns) - only the function is responsible for what happens inside it – Bergi Jun 29 '13 at 15:12
  • @Bergi, It would definitely be a bad design but why would it violate the concept of encapsulation? – tusharmath Jun 30 '13 at 14:11
  • It would be better if @Afshin shares why he wants such a feature because it seems like there is no generic, out of the box and elegant way of achieving the same. – tusharmath Jun 30 '13 at 14:14

2 Answers2

1

There is no orthodox method for doing this. You can do the opposite, however, find out who called the function a() by accessing it's arguments.callee.caller inside a().

But, if you are real serious about finding out what functions are called within your function, you can always do b.toString() and then parse the code manually :)

Lex Podgorny
  • 2,598
  • 1
  • 23
  • 40
0

You need an AST generator. Use a javascript parser like this.

tech-man
  • 3,166
  • 2
  • 17
  • 18