JavaScript - Declaring Global Scope for Nested Function?

前端 未结 5 1271
忘了有多久
忘了有多久 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 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);
    

提交回复
热议问题