1

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.

AnaMaria
  • 3,520
  • 3
  • 19
  • 36
Silom
  • 736
  • 1
  • 7
  • 20
  • 2
    Sure, it is wanted we call it "knowledge sharing" – Silom Jul 26 '13 at 07:17
  • I think you want http://programmers.stackexchange.com/ – ivarni Jul 26 '13 at 07:18
  • Please see http://stackoverflow.com/help/dont-ask: *"If your motivation for asking the question is “I would like to participate in a discussion about ______”, then you should not be asking here. "* – Felix Kling Jul 26 '13 at 07:18
  • okay I'm sorry lets say I really need a constants but don't know how to do it in the best way ... – Silom Jul 26 '13 at 07:20
  • So basically you want a variable that can not be changed after being initialized? – ivarni Jul 26 '13 at 07:21
  • 1
    possible duplicate of [Are there constants in Javascript?](http://stackoverflow.com/questions/130396/are-there-constants-in-javascript). You are probably particularly interested in this answer: http://stackoverflow.com/a/4637056/218196. For Node.js, you have to use the `global` variable instead (I assume): http://nodejs.org/api/globals.html. – Felix Kling Jul 26 '13 at 07:23
  • Yes and global would be awesome – Silom Jul 26 '13 at 07:23
  • Well you can use a closure to create a variable that you will not be able to change and access it via a function but you will always be able to change the reference to it. – ivarni Jul 26 '13 at 07:25
  • 2
    Oh, apparently Node.js does support the `const` declaration. See [How do you share constants in NodeJS modules?](http://stackoverflow.com/q/8595509/218196) (possibly a duplicate as well). – Felix Kling Jul 26 '13 at 07:27
  • Oh, didn't know about that one :) – ivarni Jul 26 '13 at 07:28
  • 1
    By now: the `const` keyword has been mentioned a couple of times, but for browser implementations, you could simply use `Object.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 – Elias Van Ootegem Jul 26 '13 at 07:32
  • @Felix thank you. The guy is telling us sth. about security interesting. I have to do some research on this subject, thanks again. – Silom Jul 26 '13 at 07:33

2 Answers2

3

It's ugly/difficult to use globals in node for a good reason: globals are bad.

Doing what you want is as easy as:

// config.json
module.exports = {
  somePath:    "/foo",
  anotherPath: "/foo/bar"
};

Use in a file

// a.js
var config = require("./config");
config.somePath; //=> "/foo"

Use in another file

// b.js
var config = require("./config");
config.anotherPath; //=> "/foo/bar"

Just recently, in another question, I went into depth about how it's completely unnecessary to use globals in node.js

Community
  • 1
  • 1
Mulan
  • 129,518
  • 31
  • 228
  • 259
  • Yes, this is how I doing it at the moment. My simple problem is just to require the config.json again and again.. – Silom Aug 19 '13 at 07:58
  • @Silom, that's not a "problem" anymore than saying I have to use `()` around a JavaScript `if` condition; that's just a part of working with node.js. Adding one line of code to call your global app config into a module is not a big deal at all. – Mulan Aug 19 '13 at 08:08
  • That is right, but this is my question. See if I write into a package.json main : "foo.js" and run node . he will run foo.js instead of index.js. So why is there nothing like a config: "config.json" and I can use it everywhere without requiring it every time. At least you are right it is not a big deal, it is just interesting to find a new easy way. – Silom Aug 19 '13 at 09:50
0

This is a bad idea, but it can be done. I've only tested this in Node.js. It may or may not work in the browser. If you substitute global for window it might work reliably in the browser.

Here you go:

app.js:

Object.defineProperty(global, 'myConst', {
  get: function() {
    return 5;
  }
})

myConst = 6

console.log(myConst) //5

Run like:

node app.js

Tested with v0.10.3.

JP Richardson
  • 38,609
  • 36
  • 119
  • 151
  • Thanks for your answer. But what could I do else ? I have like on path to a file and I want to use it everywhere. Should I do a fs.resolve in every file? – Silom Jul 26 '13 at 19:35
  • I don't understand. You can use this everywhere. Just `require` your constants in from one file in one location in your application and it'll work. For example, let's say you have a `main.js`. Just do `require('./mycontants')` at the top of `main.js`. – JP Richardson Jul 26 '13 at 19:47
  • that's the point you have to require it. If I got 10 files where I have to use it I always have to require(path/to/const.js) just to use another return value for another path. – Silom Jul 26 '13 at 20:11
  • 1
    No you don't. You only require it once. If the rest of your 10 files are required somewhere in your app, they can all access the constants if you only require it once in one of the files because of `global`. That's what I'm trying to tell you. – JP Richardson Jul 27 '13 at 21:02
  • @naomik There is a difference between a bad idea and a bad answer. Sometimes people just want to learn techniques. They have their own reasons, perhaps academic? Your analogy is awful. We're not talking about actions with irrevocable consequences. We're talking about learning. – JP Richardson Aug 19 '13 at 06:01