Javascript global variable scope issue

前端 未结 4 940
说谎
说谎 2021-01-12 21:39

I am running into a strange scope issue with Javascript (see JSFiddle):

var someGlobal = 3;

function someF() {
    // undefined issue
    alert(someGlobal);         


        
4条回答
  •  抹茶落季
    2021-01-12 22:17

    someF2 displays 3 because it still is 3.

    In someF() you create a new variable that happens to have the same name as someGlobal. That does not do anything to the original someGlobal, it just creates a new variable locally to function someF that goes away when that function ends.

    So you have local variables (e.g. created inside someF with var) and global ones.

提交回复
热议问题