What is best practice to get a variable outside of its anonymous function without polluting the global namespace?
2 Answers
A number of possibilities:
- Create a properly name-scoped public accessor function to obtain the value upon demand.
- Pass the value to the functions where it will be needed
- Pass a private accessor function to the other module
- Put the variable in a properly name-scoped global
- Pass a "data object" to the other module that has the value in it (along with other values)
Which makes the most sense depends upon how much data you need to share, how widely it needs to be shared, whether the sharing is both ways, etc...
The typical design pattern for exposing global data with the minimum impact on polluting the global namespace is to do something like this:
var JF = JF || {}; // create single global object (if it doesn't already exist)
JF.getMyData = function() {return(xxx);}; // define accessor function
JF.myPublicData = ...;
Then, anywhere in your app, you can call JF.getMyData();
or access JF.myPublicData
.
The idea here is that all public methods (or even data objects) can be hung off the JF object so there's only one new item in the global space. Everything else is inside that one object.

- 683,504
- 96
- 985
- 979
-
1I think the first option is what I'm trying to do, could you provide some detail on what this would look like? Thanks very very much. – fancy Jul 04 '11 at 04:10
-
The code you've presented doesn't make `JF` a global, though... if `JF` isn't already defined, then it's just a local object. Is that what you intend? – Trevor Burnham Jul 04 '11 at 19:04
-
@Trevor. This code creates the global: var JF = JF || {}; if that code is positioned in global scope. – jfriend00 Jul 04 '11 at 20:14
-
@jfriend00 That's true, but only if every other `var` in the same file is also in the global scope... why not use a module wrapper and omit the `var` for `JF`? – Trevor Burnham Jul 05 '11 at 16:31
-
@Trevor. If by a module wrapper, you mean something like the first code example on this page: http://imulus.com/blog/casey/javascript/coffeescript-namespaces-modules-and-inheritance/, then that's certainly fine too and many people prefer writing it that way. I think you end up with the same code in the end and the same amount of global exposure, it's just a different way of expressing it. – jfriend00 Jul 05 '11 at 17:36
There have been several CoffeeScript questions along these lines:
- How do I define global variables in CoffeeScript?
- Expose a javascript api with coffeescript
- Getting rid of CoffeeScript's closure wrapper
as well as several others that are environment-specific. If you posted a more detailed question with a concrete example, I could provide a more specific answer.

- 1
- 1

- 76,828
- 33
- 160
- 196
-
Did you know you linked to this question in your summary of past questions? hah :) – fancy Jul 06 '11 at 09:13
-
@float Whoops! Thanks, fixed. I wasn't trying to go all Douglas Hofstadter. – Trevor Burnham Jul 06 '11 at 14:53