How to call nested function in javascript? - javascript

I have two function like this..
function a() {
function b() {
alert('reached');
}
}
how i call function b() from outside of function a()..

In general, you can't. The point of defining a function inside another function is to scope it to that function.
The only way for b to be accessible outside of a is if (when you call a) you do something inside a to make it available outside of a.
This is usually done to create a closure, so that b could access variables from a while not making those variables public. The normal way to do this is to return b.
function a() {
var c = 0;
function b() {
alert(c++);
}
return b;
}
var d = a();
d();
d();
d();
var e = a();
e();
e();
e();

You can rearange your code to this
function a() {
this.b = function() {
alert('reached');
}
}
now instantiate it and run:
c = new a();
c.b();

You will need to return the inner function call.
function a() {
function b() {
alert('reached');
}
return b();
}
And then you can call this function simply by calling function a like this:
a();
The output will be the required alert.

Related

How to call a specific function from inside a subfunction in javascript?

How do I call function "a" from "a.v" without a direct reference "a()"
function a(){
alert("1");
this.v=function(){alert("hi")};
}
One way I can think of doing this without repeating a:
function a(){
let indirect = arguments.callee;
alert("1");
indirect.v=function(){ indirect(); };
}
a();
a.v();
But this does not work under strict mode and you must call a before calling a.v.
You could also do:
function a(){
alert('1');
this.v = () => this();
}
a.v = a;
a.v();
This way you don't call a() and it also works under strict mode.
Set its v property outside the function:
function a() {
alert("1");
}
a.v = function() {
a()
alert("hi")
};
a.v()

Javascript: scope of variable

For a function, does it get the variable scope during the declaration, or before the run time?
I tried the first part of code below and it doesn't work. The second part works. Can somebody shed some insight on the difference?
//// does not work
function a() {
console.log(v1);
}
function b() {
let v1 = 1;
a();
}
b();
//// does work
function a() {
console.log(v1);
}
function b() {
a();
}
let v1 = 1;
b();
OK, Let's Look at this:
//// does not work
function a() {
console.log(v1);
}
function b() {
let v1 = 1;
a();
}
b();
In the above snippet of code, you have 2 functions (a() and b()) and invoke the method b() from your "main" function (which is unnamed but there). In a(), you try to directly access a local-scope variable from b() - which you can't.
//// does work
function a() {
console.log(v1);
}
function b() {
a();
}
let v1 = 1;
b();
So in this snippet of code, you also have a() and b() - as well as a variable v1. Since this is declared in the public space (before invoking b() - which then invokes a()), it is also available within the methods that you invoke.
The let keyword gives a variable block-level scope, meaning the v1 variable in b() is only usable inside b(). a() does not know what v1 is.
//// does not work
function a() {
console.log(v1);
}
function b() {
let v1 = 1;
a();
}
b();
In this example, v1 is being set in the global scope, so all your functions will recognize it.
//// does work
function a() {
console.log(v1);
}
function b() {
a();
}
let v1 = 1;
b();

Javascript function scope and parameter

Here is the code:
function b() {
console.log(x);
};
function a() {
var x= 1;
b();
}
a();
//the output is : x is not defined!
Can anybody help explain why it will ouput undefined? I thought it would output 1. Why function b() can't get the variable x?
You're confusing closures with actual invoking of a function. If you had function b inside function a, then you can access x like so.
function a() {
var x= 1;
function b() {
console.log(x); // print out 1
};
b();
}
You must pass x variable as parameter:
function b(x) {
console.log(x);
};
function a() {
var x= 1;
b(x);
}
a();
If a var is define inside a function, the var can only be use within the function. If a var is define outside the function, then it can be use anywhere. Your var x =1 is defined inside function a(), so it can only be use inside function a(). To solve your code, you can simply move var x= 1 outside function a(). Here's the code:
function b() {
console.log(x);
}
var x= 1;
function a() {
b();
}
Or, I could suggest you use this instead. It's much shorter:
var x= 1
function b() {
console.log(x);
}

Access array from another function in javascript

Please, if is it possible to define array in one function and access this, from another function in javascript?
I think better should be:
var x =[];
function a() { x = ['a','b']; };
a();
function b() { alert(x[0]); }
b(); //alerts 'a'
Using var to declare the variable at the scope needed
Yes:
function a() { x = ['a','b']; };
a();
function b() { alert(x[0]); }
b(); //alerts 'a'

Ways to declare functions [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript: var functionName = function() {} vs function functionName() {}
What is the difference between those two functions
function a()
{
b=2;
alert(b);
}
a();
and this function
var a=function()
{
b=2
alert(b);
}
a();
what is the main difference
The main difference is that when you declare function:
function a(){
// something...
}
it becomes accessible in the same scope even before the place in the code where it is declared.
But when you assign anonymous function to a variable:
var a = function(){
// something...
};
it is not available before the assignement.
When the functions are created
It is a result of when the function is actually created. In first case it is created when the code is compiled, while in the second case the function is created when the interpreter reaches the line of assignment.
Test code
You can see the difference I mentioned above by executing the following code (jsfiddle):
try {
a();
} catch(e) {
alert('problem calling function a(): ' + e);
};
try {
b();
} catch(e) {
alert('problem calling function b(): ' + e);
};
​function a(){
alert('function a() called');
};
var b = function(){
alert('function b() called');
};​
You will see (as in mentioned jsfiddle), that a() function is called properly even before the actual declaration, but b() is not available before the assignment.
The only real difference is that the second function has no name, and that the function a() {} one is hoisted.
The difference is that function a() is defined at parse-time for a script block while var a=function() is defined at run-time.
<script type="text/javascript">
// No errors occured;
function a();
function a(){
console.log("Success");
}
</script>
<script type="text/javascript">
// An error will occured;
a();
var a = function (){
console.log("Success");
}
</script>

Categories