Top variable declared globality (ES6 Babel)

Is there a method for declaring a globaly function from a module in ES6? If I declare a variable like this:

global.variablename = function() {};
global.variablename2 = function() {};

      

These functions will be available throughout the module that I import into my main file. But how can I save all my functions to a file and import it globally? If I import it, the functions are not available from the external plugin. When they call the function, the result is undefined.

Thanks in advance!

EDIT: As I write in my comment, I have this problem: When I create an ajax form with unobtrusive ajax, I set a callback for the whole event like this:

@using (Ajax.BeginForm("Action", "Controller", null,
                    new AjaxOptions { OnComplete = "ajaxCallback"},
                    new { id = "ajaxForm", @class = "" }))

      

If I declare the "ajaxCallback" function in the main file like this:

global.ajaxCallback = function() {}

      

The callback works fine. If I put it in a module and import it (in the main file) it didn't work because it is not in the global context. I have to import it from a module, but in a global context. Is it possible?

+3


source to share


1 answer


Attaching a variable to window

will work but is not recommended, for example:

window.someVariable = 'foo'



Alternatively in webpack you can use the library property on the output via: https://webpack.github.io/docs/library-and-externals.html#examples

+2


source







All Articles