DEV Community

Cover image for Kissing JavaScript #2 globals.js

Posted on

Kissing JavaScript #2 globals.js

Have you ever asked why you must type

const { readFileSync } = require('fs')
Enter fullscreen mode Exit fullscreen mode

every time you need to read a file or use any other file handling function?

In my DRY obsession, this bothers me a lot. To me, the first requirement to write simpler code is just write less code.

One of my strategies to avoid the repetition is the use of global variables.

In first post of this series, there was a part of code I didn't commented about:

if (typeof window == 'object') 
  window.test = test
else global.test = test
Enter fullscreen mode Exit fullscreen mode

This code makes the test() function globally available, (in nodejs and in the browser) so I only need to require the file once for the entire application.

Traditionally (before ES6) if you writex = 10 not preceded by var or const, that variable will automatically become a global variable.

Having an accidental global variable is a bad thing because that variable can replace another with the same name declared in another part or library or simply leak the function scope.

For this reason, ES6 introduced the "use strict"; directive. One of the things this mode do is disallow global variables by default.

After that, most of the libraries avoided using global variables to not pollute the global space.

So, I've a good news to you:

Now the global space is almost desert and is free to be used at will by you. Yes you are the owner of global space now, and you can use it to make you life simpler.

So my second tip is just this: Create a file named globals.js and put on it everything you want to have always at hand.

Follow a model with part of my globals.js, with some ideas of nice globals:

// check if the fs variable already exists in the global space, if so, just returns
if (global.fs) return 

// a shortcut for the rest for the file
var g = (typeof window == 'object') ? window : global

g.fs = require('fs')
g.fs.path = require('path') // make path part of fs

g.json = JSON.stringify
g.json.parse = JSON.parse

// from the previous article
g.test = require('./test.js') 

// plus whatever you want always available
Enter fullscreen mode Exit fullscreen mode

Now just put in the main file of your NodeJS project, the line

Enter fullscreen mode Exit fullscreen mode

and after that in anywhere of your project when you need a function of fs module, you just need to type:

var cfg = fs.readFileSync('cfg.json')
Enter fullscreen mode Exit fullscreen mode

without any require().

I know this is not the most complex or genial article you have ever read on here, but I'm sure the wise use of global space can save you a lot of typing.

A last word:

In this time of so many bad news, I want to give you another little tip: Turn off the TV, and give a tender kiss in someone you love and loves you (despite the distancing propaganda we are bombarded). Say her how important she is to you life and how you would miss if she is gone. (The same to him)

I my own life, every time I faced death I realized that the most important and the only thing that really matters and we will carry with our souls to the after life is the love we cultivate.

So, I wish you a life with lot of kisses and love.

From my heart to your all. 😘

Top comments (0)