Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to define global variable in ES6 modules?

I can't seem to find a description of the way I should export global variable from ES6 module. Is there a resource where it's defined?

The only solution that seems to work is referencing a global object, like window:

window['v'] = 3; 

But what if this scripts runs in Node.js? Then I don't have window; I have global. But this code is not good:

var g = window || global; g['v'] = 3; 

I understand the concept of modules and don't use globals in my applications. However, having global variables during debugging in the console may be beneficial, especially when using bundlers like Webpack instead of loaders like SystemJs where you can easily import a module in a console.

like image 266
Max Koretskyi Avatar asked Apr 25 '17 08:04

Max Koretskyi


People also ask

How do I declare a global variable in es6?

It's like a global variable, and it should be straightforward if we have everything in one file, for example: var globalTooltips = []; function veryComplicatedProcess(data){ //some complicated logic const newData = shapeMe(data) saveData(newData); // additional task const tooltips = getTooltips(newData.

How do you define a global variable?

In computer programming, a global variable is a variable with global scope, meaning that it is visible (hence accessible) throughout the program, unless shadowed. The set of all global variables is known as the global environment or global state.

How do you define a global variable in JavaScript?

Global Variables are the variables that can be accessed from anywhere in the program. These are the variables that are declared in the main body of the source code and outside all the functions. These variables are available to every function to access. Var keyword is used to declare variables globally.

Which is the correct format to declare global variable and constants in JavaScript?

Correct way to declare global variable in JavaScript The proper way is to use window object. And use the syntax like this: var window. iAmGlobal = "some val" ; //Global variable declaration with window.


1 Answers

There are several ways to have global values available in your application.

Using ES6 modules, you can create a constant which you export from your module. You can then import this from any other module or component, like so:

/* Constants.js */ export default {     VALUE_1: 123,     VALUE_2: "abc" };  /* OtherModule.js */ import Constants from '../Constants';  console.log(Constants.VALUE_1); console.log(Constants.VALUE_2); 

Alternatively, some JS bundling tools provide a way to pass values into your components at build time.

For example, if you're using Webpack, you can use DefinePlugin to configure a few constants available at compile time, like so:

/* Webpack configuration */ const webpack = require('webpack');  /* Webpack plugins definition */ new webpack.DefinePlugin({     'VALUE_1': 123,     'VALUE_2': 'abc' });  /* SomeComponent.js */ if (VALUE_1 === 123) {     // do something } 
like image 95
Shishir Avatar answered Sep 28 '22 03:09

Shishir