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.
Related
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()....
}
This question already has an answer here:
Function in if condition clause
(1 answer)
Closed 6 years ago.
So i have the following code:
(function init_chart(){
// body function
})();
it's working as expected. but when i tried to call the init_chart() from outside the function, console said it is undefined. then i tried to add the following line inside init_chart() function:
window['init_chart'] = this;
now console said init_chart is not a function.
You are creating a named function expression function, so basically the function is not defined on the global scope. The easiest way to get around it is to do the following:
function init_chart(){
// body function
};
init_chart();
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.
This question already has answers here:
what is the difference between calling function in JavaScript with or without parentheses ()
(4 answers)
Closed 9 years ago.
A simple question from someone trying to learn:
I have this:
$(function(){$("#topFlag").hover(changeFlag, setFlag ); });
function changeFlag(){
//some code
};
function setFlag(){
//somecode
};
And it's all working (now). But what I expected to use was:
$(function(){$("#topFlag").hover(changeFlag(), setFlag() ); });
What's the difference? Why doesn't changeFlag() (with the parens) work? Isn't this a function call? What if I wanted to pass a parameter to the function?
Thanks for any insights (or pointers to documentation I can read). I've already checked out:
http://api.jquery.com/hover/
http://www.w3schools.com/js/js_functions.asp
changeFlag is a function.
changeFlag() calls that function.
You need to pass the function. You don't need to call the function and pass its return value.
When you add braces after a function name , it executes the function
setFlag() ; // calls the function
But you want the function to fire , when you hover over the element
And not at the time of attaching the event
In javascript the functions are also variables, when you pass it as a parameter you want to send the function to execute, if you write this yourFunc() you would be sending the result of that function instead.
To send parameter I use this:
$(function(){$("#topFlag").hover(function(){changeFlag(param1, param2,...)}, function(){setFlag(param1, param2,...)}); });
this creates an anonym function that call your functions.
This question already has an answer here:
Anonymous function vs normal function
(1 answer)
Closed 9 years ago.
I know this type of question gets asked a lot, but I haven't seen any question with this type of declaration
(function(){
myFuncName=function(myVar){
// some logic
};
}());
how does that differ from
function myFuncName(myVar){
// some logic
}
The first one is an anonymous function and there is no way you can reference to and call it later on, so you just executes instantly ( ) once it had been created!
(function(){
alert(1)
}())
The second one is a reference function that you can call it anytime later. It wont gets executed unless you call it explicitly
What your doing is creating an anonymous function that also has a closure in it. Read more about closures here.
Basically a closure means that you have declared a function inside of another function. Which will let you access local variables after the first function exits.
Normally you would not be able to do this since they would be out of scope.
As for the other part. You can find a very useful guide to what's happening here Why do you need to invoke an anonymous function on the same line?.
To sum it up though, you have created an anonymous self-invoking function expression.
The self-invoking comes from the fact that () immediately follows the function expression.
Here's a link which explains the the function declarations in javascript.