0

I have a situation like this:

console.log(someVar) // <- works, the variable is defined
var somefunction = function () {
    console.log(someVar); 
}

somefunction(); // <- someVar is undefined on the console

I can't demonstrate a simple model showing this because I don't even know how this is possible. If someVar is defined in the context where someFunction is defined, isn't someVar also in the scope of someFunction?

Are there situations where it is supposed to work like this? If so, I may have stumbled into one accidentally.

AdamHolder
  • 61
  • 5
  • 1
    You should not be returning anything - you have no return method. You are just logging an undefined var. – Korgrue May 12 '16 at 21:50
  • 2
    "I can't demonstrate a simple model showing this because I don't even know how this is possible." — You clearly have some code which does demonstrate it. [Reduce it](http://stackoverflow.com/help/mcve) until it makes for a sensible test case. – Quentin May 12 '16 at 21:51
  • Fine, I used the wrong word. That's hardly relevant to the question. – AdamHolder May 12 '16 at 21:51
  • @AdamHolder — Since the function actually does *return* undefined, it is quite relevant. – Quentin May 12 '16 at 21:52
  • @Quentin Meanwhile, can anyone explain whether there is supposed to be any situation at all where this is possible? – AdamHolder May 12 '16 at 21:53
  • It's a nitpicky comment. Obviously I was talking about what is produced int he console based on the console.log function. I'll correct it so that nobody will pretend they misunderstood. – AdamHolder May 12 '16 at 21:54
  • @AdamHolder — I can think of various reasons, but I'd have thought they'd be pretty obvious, and I'm not going to speculate when you don't have a test case that demonstrates the problem. – Quentin May 12 '16 at 21:55
  • I have a suggestion. How about not coming here, asking for help - and then responding with snarky comments when people try to help you. – Korgrue May 12 '16 at 21:56
  • I wasn't snarky to anyone who was helpful. Or even trying to be helpful. As usual, I was accosted by people trying to tear me down instead of offering anything helpful or constructive. That is what happens here on StackOverflow 90% of the time. – AdamHolder May 12 '16 at 22:02
  • That's fine - your question has been reported as incomplete - so you wont be getting the help you are looking for anyways. Maybe next time you could word your questions in a way that people can actually understand what you are trying to do. – Korgrue May 12 '16 at 22:04
  • 2
    @AdamHolder — Once again: If you provide a proper test case then people can help. If you show us code which works but claim that it doesn't work, then we can't do a lot. – Quentin May 12 '16 at 22:05
  • The first answer of this question might explain the behavior you are seeing. http://stackoverflow.com/a/111111/3103677 – sebenalern May 12 '16 at 22:07
  • `someVar` in the scope of `someFunction` is the same as in the global scope. Since `someFunction` doesn't return a value (how EVERY function should do), it implicitly returns `undefined`. I guess you're confusing this `undefined` with the output of `console.log` within `someFunction`. –  May 13 '16 at 19:12

2 Answers2

0

The variable named had been shadowed further down in the function in a non-obvious way.

Flexo
  • 87,323
  • 22
  • 191
  • 272
AdamHolder
  • 61
  • 5
-1

JS

var somefunction = function () {
    var somevar = "foo";
    return somevar;
}

alert(somefunction());

Fiddle

Korgrue
  • 3,430
  • 1
  • 13
  • 20
  • The OP has clarified that they meant the `console.log()` statement inside the function did not get the right value, not that it had anything to do with actually returning the data. In fact, they did that a good three minutes before you submitted this answer. – Quentin May 12 '16 at 21:56
  • It is returning the proper data. OP never defined the variable in his example. The question was about scope. The solution above demonstrates how to scope his object and get something other than undefined when he checks the output. – Korgrue May 12 '16 at 22:01
  • The question shows that, while the variable is defined somewhere before the code snipped begins, it has to be defined already (because otherwise the first console.log would output undefined). So the problem is not that it isn't defined. – Quentin May 12 '16 at 22:04
  • Thus the problem with not providing sufficient source and not wording the question properly. OP gets incomplete or bad answers. – Korgrue May 12 '16 at 22:08
  • 1
    In theory they should get close votes instead of bad answers. – Quentin May 12 '16 at 22:08
  • I don't disagree with that. – Korgrue May 12 '16 at 22:12