Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

the use of const in node.js at least can be used today and I think should be for all required modules... for example, it's always bothered me in node.js that most examples are written in the following way:

var fs = require('fs');

fs is such an easy name to give to any random variable in a function

function foo() { var fs = 88; // just overwrote fs module.. }

I do two things in my non-standard way...

const FS=require('fs');

it makes more sense to me... i'm less likely to write in a random function:

function foo() { var FS = 88; }

Just my thought... node.js supports const for a long time I wish examples would use it for required modules...



But your first example doesn't overwrite fs, it just overwrites the use within that function scope. Which is what you want (to use the fs that you assigned). Outside that function fs is still fs.

const is really just a readonly. It would prevent you from accidentally overwriting fs if you were to forget to type var at some point.

More needs to be written about when to use const, because there isn't much explanation for it out there. It seems like engines can better optimize some code if it knows certain variables won't be reassigned. But in that case you mind up always using const, which I doubt is correct either. There has to be some overhead to using const, so you probably wouldn't want to use it in a tight loop, for example. Or maybe I'm all wrong about this. Need some writing.


My example, was perhaps too simple seeing how the functions are very short... I'm sure someone will mention making long functions is a bad idea... but it happens... using const for required modules seems like a simple rule to follow for avoiding trouble... Also, stylistically using an uppercase lettering while not normal in node.js community is normal to JS in other contexts... jQuery... getElementById.. perhaps Fs if you prefer however looking through mozilla code as a guideline - variables declared const are uppercase... hence FS makes better sense to me... because why would a module variable name ever not be... and in the rare case that it is.. declare it var at least until let is available...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: