Get inside function calls [duplicate] - javascript

This question already has answers here:
How to generate call-graphs for given javascript? [closed]
(4 answers)
Closed 9 years ago.
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?

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 :)

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

Related

Functions that call eachother [duplicate]

This question already has answers here:
Are variables statically or dynamically "scoped" in javascript?
(7 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 2 years ago.
I am looking for someone to explain me something since I can't find a clear answer. My question is about functions that call themselves. I saw that it is possible to build a list of functions that are 'chained' together and for example the first function calls the second one then the second one calls another one. My confusion is : Lets say that you have a second function that has a variable let a = 12; if i call that function on the first function, will i have access to that variable or whatever that second function might have inside? How can i pass info to another function? can a function can be dependent on another function in order to complete a task? Thanks in advance guys.
Edit to make it more clear what I mean:
function first(){
second();
// will i have access to whatever there is inside function second since i am calling it here ? or it doesn't work that way?
}
function second(){
let a = 12;
third();
}
function third(){
fourth()....
}

Difference between function declerations [duplicate]

This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 2 years ago.
What is the difference between this (a 'standalone' function):
function standaloneFunction () {
console.log('standaloneFunction runs, success')
}
standaloneFunction()
...and this (a function inside a variable):
let variableFunction = function variableFunction () {
console.log('function inside let has same name as the variable, runs? - yep, does.')
}
variableFunction()
Is it a problem that the variable and the function share the same name?
it doesnt seem so - i speculate this is because it has something to do how variables
and functions are saved in memory? Functions in their entirety, and variables only their declaration?
When i do console.log(this), i can't find the 'variableFunction' in the execution
context of 'this' - however, 'standaloneFunction' does appear.
Have you, as a beginner, also asked yourself such questions? Am i being too picky about such details?
Should i already use es6 syntax?
Please also don't hold back with any advice regarding articulating my question.
Thanks to everyone who has taken their time to read this.
The first is a function declaration, which will be hoisted. The second is a named function expression, but I think an anonymous function expression would be better in this case since there’s no need to name the function, e.g. let variableFunction = function() {…}.
Please see What is the difference between a function expression vs declaration in JavaScript?

Using the function inside a function in Javascript [duplicate]

This question already has answers here:
Javascript call nested function
(11 answers)
Closed 6 years ago.
Can anyone please help me in this? I have declared a function inside a function and now want to call only that function.
For example:
function hello(){
alert("Hello");
function insideHello(){
alert("insideHello");
}
}
I just want to call the insideHello function.
I know one way is to call (new hello()).insideHello(); by declaring this.insideHello = function. I don't want to use new every time because I am using this in canvas scenario.
You could make hello a "module" that exposes insideHello as part of its API:
function hello() {
alert("Hello");
function insideHello() {
alert("insideHello");
}
return {
insideHello // or insideHello: insideHello
}
}
hello().insideHello()
I would have two functions and you call the second one from inside the first - but you can call the second one separately as well (of course the names wont mean much in this example).
function hello(){
alert("Hello");
insideHello();
}
function insideHello(){
alert("insideHello");
}
That way you can get both the outer and inner function by calling hello(); and just the inner function by calling insideHello(); ... again noting that the names are not very descriptive since i removed the inner function to the outside. But it seems that if you want to only call the inner one then you shouldn't need to call the outer one. and if you want to call both then the outer one should be able to handle that.

Function excuted before creation [duplicate]

This question already has answers here:
Why can I use a function before it's defined in JavaScript?
(7 answers)
Closed 9 years ago.
I am no JavaScript expert, but I found some code like this
a();
function a(){
alert('a');
}
and I was surprised to find it works (I think something like that will not work in Python). I expected that the function a can't be executed before being created. How does interpreter work and why functions can be called before declaration?
This happens because of variable hoisting.
See this answer for more info
JavaScript 'hoisting'
Some docs about this:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var#var_hoisting
If you type it like this, it won't work:
a();
a = function(){
alert('a');
}
Code that is within functions and objects will be run whenever that
function or object is called. If it is called from code that is
directly in the head or body of the page then its place in the
execution order is effectively the spot where the function or object
is called from the direct code.
See the reference here.
And in our case the function will give the error as you can see the example here.
It's because function a() is declared via Function Declaration syntax, and Function Declaration are executed right after the script is parsed. With other syntax, Function Expression, like this:
var b = function(){
alert('b');
}
it won't work (see example).
More info: http://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/

What this mean in javascript? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Explain JavaScript's encapsulated anonymous function syntax
I have just read a javascript book but I have seen this code:
1(function() {
// code
})();
what is this ? is a special function ?
As written, it has a syntax error.
I'm guessing it was more like:
(function() {
// code
})();
or
(function() {
// code
}
)();
Break it down:
(FOO)() // calls FOO with no arguments.
And
function() { //creates a function that takes no arguments.
// code
}
Hence together it would create a function that takes no arguments, and then call it. I can't see why you would apart from just showing that you can.
It looks like the intent was to declare the function inline/anonymous and immediately execute it.

Categories