-2

Usually, defining a variable outside functions is enough to let it be "global". In my case, however, situation seems to be different.

var username = null;

function myFunction_1() {
    username="pippo";
    myobject.myfunction(function (data){ username="mickey" })
    console.log("1: ", username);
}
myFunction_1();

I expected this code to log "1: mickey". However, if i set the variable inside a callback function logs "1: pippo", that is setting inside the callback gets ignored. What i'm missing? Setting variable in this way is not enough?

Sasha Grievus
  • 2,566
  • 5
  • 31
  • 58
  • 2
    do you mean `myFunction2` by second? – Mritunjay Jul 23 '14 at 14:02
  • 3
    And what is `salvaUsername`? – Andy Jul 23 '14 at 14:02
  • 1
    The problem is inthe code you did not show. If you use `
    ` you should see "3: pippo"
    – Igor Jul 23 '14 at 14:06
  • yes, sorry, my error, i meant
    . (I'm sorry, but 2 downvotes for distraction seems to me a bit pitiless ^^')
    – Sasha Grievus Jul 23 '14 at 14:22
  • I [can't reproduce the problem](http://jsbin.com/yupegovu/1/) - it reports `3: pippo` when the form is submitted. – Quentin Jul 23 '14 at 14:28
  • Yea Igor and Quentin it's so. I tried to simplify a problem that was a bit more complicate that it seemed. It works if tested in the way i described it. My real problem it's another, i'll try to edit the question so to explain... – Sasha Grievus Jul 23 '14 at 14:30
  • What is `myobject`? What is `myfunction`? – Quentin Jul 23 '14 at 14:42
  • Of the three versions of this question you've asked, two error and none show the problem you are asking about. You need to provide code in the question that actually reproduces the problem. – Quentin Jul 23 '14 at 14:44
  • The code is really too complicate, that's the problem. I thought it was not so important what myfunction or myobject were. Even if i change the variable in a callback it should get the value? – Sasha Grievus Jul 23 '14 at 14:57
  • Assuming the callback gets called before you next inspect the variable. Since it clearly isn't, we'd need to see `myfunction` (and probably `myobject`) before can tell why it isn't. – Quentin Jul 23 '14 at 15:04
  • If it can help i'm totally sure The callback get called before for The function trying to access that value is called when i push a submit button in a form and i know when tha callback gas been called for it logs a confirmation message. As soon as i can i'll try to post a simplified version of The callback. Thank you – Sasha Grievus Jul 23 '14 at 15:56

2 Answers2

0

Assuming that code is not inside a function you haven't shown, username is a global variable. If you look at the value of username before calling myFunction_1, it will be null. If you call myFunction_1, then look at username, it will be "pippo" (and it will stay "pippo" until you do something not shown to change it). So if you're seeing null when you expect "pippo", it tells you that either A) myFunction_1 has not (yet?) been called, or B) You've done something since calling myFunction_1 that set username back to null.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Your code is being executed from the top of the page as following :
username gets declared and set to = null -> myFunction_1() get's defined -> myFunction_1() gets called -> username gets set to 'pippo' -> console.logs "1: pippo" -> console.logs "2: pippo" -> myFunction_2() get's defined -> myFunction_2() gets called -> console.logs "3: pippo" this happens in this sequence assuming that this code runs, which it does not in your case.

Assuming that salvaUsername() looks like function salvaUsername(){ return username; } username is null as it have never reached the point of assignment that happens in myFunction_1(). (It's actually surprising that output is not undefined but null).

UPDATE In this case myFunction_1() never runs so username doesn't get set to 'pippo' hence the outcome.

Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265