Top variable declared globaly (ES6 Babel)

Exist a method to declare a function globaly from a module in ES6? If i declare a variable like this:

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

This functions will be avaiable in all module that i import in my main file. But how can i save all my functions in a file and import it globaly? If i import it the functions are not accessible from an external plugin. When they call the function, it result 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 the callback for the complete event like this:

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

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

global.ajaxCallback = function() {}

The callback work fine. If i place it in a module and import it (in the main file), it didn't work because isn't in the global context. I must import it from a module but in the global context. Is it possible?

Answers


Attach the variable to window would work but not recommended such as:

window.someVariable = 'foo'

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


Need Your Help

Avoid push/addToSet null value to mongodb array

mongodb mongoose

When using $push or $addToSet operator in mongoose, and the value I give (friend.id) is null or undefined, it will push a null element to the array.

Is it possible to pass the parameter in php execution?

php exec

Hi I am trying to run a C binary program in a php script.