0

This is simple, but I cannot understand why it does not work.

var a = function() {
	console.log(c); // "10" is expecting, but it is not defined
};

var b = function() {
	var c = 10; // local variable, but it is accessibly for b(), I think
	a();
}();

Why is variable c not defined? And how can I resolve this problem without passing c into b() as an argument?

3
  • 1
    c is local to b, it's not visible to a. c needs to exist outside of b or you need to pass it to a. Commented Nov 23, 2015 at 19:08
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/… Commented Nov 23, 2015 at 19:09
  • Because C live in B and it can not be accessed from outside B You can pass C as a PARAM var a = function(c) { console.log(c); // "10" is expecting, but it is not defined }; var b = function() { var c = 10; // local variable, but it is accessibly for b(), I think a(c); }(); Commented Nov 23, 2015 at 19:10

5 Answers 5

1

this way:

var c = 0;
var a = function() {
    console.log(c); // "10" is expecting, but it is not defined
};

var b = function() {
    c = 10; // local variable, but it is accessibly for b(), I think
    a();
}();
Sign up to request clarification or add additional context in comments.

1 Comment

@RahulDesai sure, in this case it will be undefined
1

The question is better viewed from the other side. Why would it be defined? a wouldn't really be so reusable with a comment saying "NOTE: You can only run this function if you have a var c declared in the encompassing function". That's just not very discoverable, and means it's very hard to track how long a certain naming scope is used.

A feature of JavaScript that may do what you desire is "closures". This is when you declare an entire function inside of another; it's very handy for callbacks when you don't feel the second function deserves its own naming and place in the code structure.

What will happen here is that the language will automatically see that it needs c (already declared) inside of the function you're declaring, and so it preserves an internal reference to it, associated with the callbackFunction variable. So, you can still refer to it in there.

var b = function() {
    var c = 10; // local variable, but it is accessibly for b(), I think
        var callbackFunction = function() {
           console.log(c);
        };
        // optionally, place c in a setTimeout, ajax callback, etc.
    callbackFunction();
}();

Comments

1

The function a has a new scope in which c isn't defined. You could define var c = 0; outside of a or pass c as argument to a.

Comments

1

you forgot to add parameter to function a

var a = function(c) {
    console.log(c); // "10" is expecting, but it is not defined
};

var b = function() {
    var c = 10; // local variable, but it is accessibly for b(), I think
    a(c);
}();

4 Comments

You forgot it too. :)
This will not work. You are not passing c into a(); and it will return undefined
Yes lol i've missed that too :)) actually i missed the point of the question :)
Could also use console.log(arguments) at a , without declaring possible parameter c
0

function inherits parent scope where it was declared, not where it was called. All functions inherit window scope, so you can just make variable c global.

var a = function() {
    console.log(c); // "10" is expecting, but it is not defined
};

var b = function() {
    c = 10; // local variable, but it is accessibly for b(), I think
    a();
}();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.