12

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...

2
  • 1
    I don't think you need an account to comment on answers to your question. Commented Mar 4, 2011 at 20:01
  • You can always register. Won't hurt. :) As you can see there are lots of very prominent users on Stackoverflow (ie. Jon Skeet, Joel Spolsky, Jeff Atwood...) Commented Mar 4, 2011 at 20:04

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

4

What about:

function A() {
    window.B = function() {
        alert("function B is running");
    }
}
//CALL FUNCTION B FROM GLOBAL SCOPE
B();

Comments

3

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);

Comments

0

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

Comments

0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.