Access array from another function in javascript - 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'

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);
}

How to call nested function in 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.

How do i pass my scope correctly to look for variables

i was trying to use the javascript APPLY function to pass scope from 1 function to another, but it seems that i might be doing it wrong?
Here is a fiddle if you need it: http://jsfiddle.net/C8APz/
Here is my code:
function a(){
var x = "hello";
b.apply(this, []);
}
function b(){
console.log("x: ", x);
}
a();
i was thinking that while the scope is passed, the variables / variable reference are not.
Is there a way to do something like this without defining Globals?
Should i add the data to the actual part of it, such as this.x = x;? and then in the other function just fetch it? var x = this.x;
function a(){
var x = "hello";
this.x = x;
b.apply(this, []);
}
function b(){
var x = this.x;
console.log("x: ", x);
}
a();
Edit: It seems that the second example assigns in the global scope, which isnt good, and with the scope, i was attempting to pass an understanding of context to. It seems that you really have to define a context before you pass it, otherwise, for the most part this refers to window
You cannot pass scope around.
You can either move the function declaration for b inside the function declaration for a so that the scope is right to start with, or you can pass the variables you care about using arguments.
function a(){
var x = "hello";
b();
function b(){
console.log("x: ", x);
}
}
a();
function a(){
var x = "hello";
b(x);
}
function b(x){
console.log("x: ", x);
}
a();
Declare var x; out of the function body...
var x;
function a(){
x = "hello";
b.apply(this, []);
}
function b(){
//a.apply(this,[]);
console.log("x: ", x);
}
a();

Categories