JavaScript - Declaring Global Scope for Nested Function?

前端 未结 5 1273
忘了有多久
忘了有多久 2021-02-14 00:36

My attempts at giving global scope to a nested JavaScript function are not working:

//DECLARE FUNCTION B IN GLOBAL SCOPE
function B;

function A() {

    //DEFIN         


        
相关标签:
5条回答
  • 2021-02-14 00:58

    There appear to be a couple of issues with your code

    1. The first line doesn't appear to be legal Javascript (JSLint agrees). To declare an uninitialized variable use the var B; syntax
    2. The code never calls A to initialize B so calling B() is invoking an uninitialized variable
    3. I'm fairly certain the code to initialize B inside of A is also not legal.

    Try the following

    var B; // Establish B as a global scope variable
    
    function A() {
      B = function() {
        alert('B is running');
      };
    }
    
    A(); // Call the function which will cause B to be initialized
    B(); // Run B
    
    0 讨论(0)
  • 2021-02-14 01:00

    What about:

    function A() {
        window.B = function() {
            alert("function B is running");
        }
    }
    //CALL FUNCTION B FROM GLOBAL SCOPE
    B();
    
    0 讨论(0)
  • 2021-02-14 01:03

    You're close, but not completely correct.

    1. You have to define B as a variable and then assign a function to it.
    2. Also run A() before executing B, otherwise B will be undefined. The easiest way of running it is the way that I show in my code example.

    These are the smallest amount of changes to your code to make it work as you asked:

    var B;
    
    (function A() {
        // define function B
        B = function() {
            alert("function B is running");
        }
    })();
    
    // call function B globally
    B();
    
    0 讨论(0)
  • 2021-02-14 01:06

    You can do something like this:

    function outer() {
      function inner() {
        // ..
      }
    
      window['inner'] = inner;
    }
    

    It's a little icky to have a direct reference to "window", so you could do this (from the global context):

    (function (global) {
      function inner() {
        // code code code ...
      }
    
      global['inner'] = inner;
    })(this);
    
    0 讨论(0)
  • 2021-02-14 01:16

    function B; will simply generate a syntax error.

    You can use a function expression. As functions are first class objects, you can assign a function to a variable:

    var B; // declare (global) variable (outer scope)
    
    function A() {
        // assign a function to it
        B = function() {
            alert("function B is running");
        };
    }
    
    // we have to call A otherwise it won't work anyway
    A();
    // call B
    B();
    

    You could also let A return a function:

    function A() {
        return function() {
            alert("function B is running");
        };
    }
    
    B = A();
    

    This would make the relation between A and B a bit clearer.

    Of course you can always define a global variable by omitting var, but you should use this very carefully. Use as less global variables as possible.

    function A() {
        B = function() {
            alert("function B is running");
        };
    }
    

    And I bet there is a better way of doing it, depending on what your actual goal is.


    More about Functions and function scope.

    0 讨论(0)
提交回复
热议问题