In Javascript, can constants be shared between files, as in Ruby? -
this odd question because it's more typical people ask how avoid using globals.
coming ruby world, i've become comfortable using globals in 2 specific examples:
constants. when file imported in ruby, of constants automatically made available other files in program.
(and ties in first) packages. when load ruby gem in required file, becomes available in other files.
i've been starting use module.exports
, i'm finding i'm importing same modules in lots of different files.
i'd have these features in javascript. way i'm writing code @ moment, i'm using functional approach , passing constants parameters. problem code getting verbose liking.
i'm not looking "short answer: no" type of response, here. if difficult, i'd appreciate being pointed in direction how avoid passing constants parameters functions.
one method of using globals use html5 local storage.
my thinking is, have object globals, , on page load save each global variable own local storage location.
so have object globals stored:
var globals = { global1: "somestring", global2: 400 }
then onload
/ or if want sooner have called before page loads, can have function run through globals , save values local storage
for(var key in globals) { localstorage.setitem(key, globals[key]); }
then, later on, when function needs, example global2
can call:
localstorage.getitem("global2");