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);
}
Related
I am new to closure concept. Can somebody explain me how closure works in the below function.
As per my understanding, function b() can access 'var y' if it is defined within A() and not under function a(). But it accesses the variable value though it is within another function.
function A(){
function a(){
var y = 'some text!!';
return function(x){
console.log(y);
}
}
function b(someVar){
var buffElem = [];
someVar(buffElem);
}
var myFun = a();
var mySecFun = b(myFun);
} A();
As per the above function,
myFun = function (x){
console.log(y);
}
How does myFun understands what is y?
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.
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'
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();
Quick question: How do I get a pointer to a pre-declared function in javascript.
Say in my .js file I have
function a() {
var bref = `a pointer/ref to b() here`
}
function b(param) {
...
}
I want bref to be simply a pointer to the function b and not just the result of a call to b()
Edit: Forgot to mention: function b takes in one parameter i.e b(param)
Have tried bref = b
Just use the function name for assignment.
To call the function you can use
call
or
apply
methods.
e.g:
function a() {
var bref = b;
.
.
b.call(null, "Something")
}
function b(src) {
...
alert(src);
}
Working example #: http://jsfiddle.net/UasYH/1/
You can simply:
function a() {
var bref = b;
...
x = bref("cake");
}
function b() {
...
}