how to get the value from a callback function

后端 未结 2 1756
一个人的身影
一个人的身影 2021-01-27 06:58

I am relatively new to javascript and I am facing some difficulty.I have two java script files as I have shown below. I am having trouble getting the value of the variable

2条回答
  •  攒了一身酷
    2021-01-27 07:17

    Eventhough it will make your code a mess, you can append the variables to the window object. For example:

    function a()
    {
     window.testStr = "test";
    
    }
    
    function b()
    {
     alert(window.testStr);
    }
    

    Or even create your own object, instead of using window, as such:

    var MyRSSReader = {
     TitleOne : '',
     TitleTwo : '' 
    } 
    
    MyRSSReader.TitleOne = "My title";
    

    Wikipedia has a nice article about global variables, and why they are bad.

提交回复
热议问题