Calling a function that's defined inside a function

后端 未结 3 662
盖世英雄少女心
盖世英雄少女心 2020-12-16 02:26

*Is there a way to call a function defined inside another function in javaSCRIPT? For example:

window.onload() = function() {
    function my_function(){
            


        
3条回答
  •  抹茶落季
    2020-12-16 03:10

    The scope of the function is the core issue here, as Zeychin and Trevor have said. I thought I'd offer another way of handling it. Basically, you can set your function to a variable that's in a higher scope (that is, accessible to both the onload and function_two functions), while defining it inside the onload function as you originally have:

    var myFunction; //This is the placeholder which sets the scope
    
    window.onload() = function() {
       myFunction = function() { //Assign the function to the myFunction variable
          print('blah');
       }
    }
    
    function function_two() {
       myFunction();
    }
    

    This might be handy if you only know the information you need for myFunction once you're in the onload event.

提交回复
热议问题