JavaScript - Declaring Global Scope for Nested Fun

2019-03-25 13:55发布

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

//DECLARE FUNCTION B IN GLOBAL SCOPE
function B;

function A() {

    //DEFINE FUNCTION B INSIDE NEST
    B() {
        alert("function B is running");
    }
}

//CALL FUNCTION B FROM GLOBAL SCOPE
B();

This is just curiosity -- you are correct that I don't really have any good reason to want to do this.

TIA -- I don't have an SO account to respond to your answers...

5条回答
Anthone
2楼-- · 2019-03-25 14:28

What about:

function A() {
    window.B = function() {
        alert("function B is running");
    }
}
//CALL FUNCTION B FROM GLOBAL SCOPE
B();
查看更多
叼着烟拽天下
3楼-- · 2019-03-25 14:39

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
查看更多
一纸荒年 Trace。
4楼-- · 2019-03-25 14:39

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();
查看更多
Explosion°爆炸
5楼-- · 2019-03-25 14:44

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.

查看更多
贪生不怕死
6楼-- · 2019-03-25 14:45

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);
查看更多
登录 后发表回答