javascript - Accessing variables from another CoffeeScript file? -


what best practice variable outside of anonymous function without polluting global namespace?

a number of possibilities:

  • create name-scoped public accessor function obtain value upon demand.
  • pass value functions needed
  • pass private accessor function other module
  • put variable in name-scoped global
  • pass "data object" other module has value in (along other values)

which makes sense depends upon how data need share, how needs shared, whether sharing both ways, etc...

the typical design pattern exposing global data minimum impact on polluting global namespace this:

var jf = jf || {};  // create single global object (if doesn't exist) jf.getmydata = function() {return(xxx);};   // define accessor function jf.mypublicdata = ...; 

then, anywhere in app, can call jf.getmydata(); or access jf.mypublicdata.

the idea here public methods (or data objects) can hung off jf object there's 1 new item in global space. else inside 1 object.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -