What do you guys think about making constants in Javascript. I want to do it in the best possible way. I know that constants do not really exist but I wrote this and I was not able to change the values after the export.
Is there a need for constants?
Is there a work around?
How can we use them global (without require('const')); ?
// Const
var constants = {
'PATH1' : __dirname + '/path..../',
'PATH2' : __dirname + '/path/..../'
};
module.exports = function(key) {
return constants[key];
};
//console.log(constants('PATH1'));
Would be glad if I get some feedback, like your thoughts about these questions.
I wish you a good day.
globalvariable instead (I assume): nodejs.org/api/globals.html.constdeclaration. See How do you share constants in NodeJS modules? (possibly a duplicate as well).constkeyword has been mentioned a couple of times, but for browser implementations, you could simply useObject.defineProperty(constants, 'PATH1', {value: 'theValue'});because the property defaults to non-configurable, non enumerable and non-writeable, it effectively turns into a read-only, constant value property