Created
January 18, 2011 20:38
-
-
Save ryankshaw/785094 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I think we should standarize on one of these (or something you come up with) methods of managing our modules. | |
// Goals: | |
// 1. make as few global variables as possible | |
// 2. make it obvious what globals a certain module depends on. | |
// 3. do those two in the easiest to read, least ammout of boilerplate way as possible. | |
// | |
// suggestions: | |
var globalThing, OtherGlobalThing; | |
var myModule = (function() { | |
var privateVar = 0; | |
return { | |
next: function() { | |
return id++; | |
}, | |
reset: function() { | |
id = 0; | |
} | |
}; | |
})(); | |
// ...or | |
// same, but you can build up your returned publicMethods object as you go. | |
var globalThing, OtherGlobalThing; | |
var myModule = (function() { | |
var privateVar = 0; | |
var publicMethods = { | |
hi: function(){}, | |
otherMethod: function(){} | |
} | |
publicMethods.next = function(){ | |
return privateVar++; | |
} | |
return publicMethods; | |
})(); | |
// same as first suggestion, with a different way of specifying globals. | |
// using this method I think makes it a little more clear that we are importing globalVariables and | |
// allows closureCompiler to munge the name of the global objects | |
// I also like it because the only thing outside of the closure is "var myModule" | |
var myModule = (function(globalThing, OtherGlobalThing) { | |
var privateVar = 0; | |
return { | |
next: function() { | |
return id++; | |
}, | |
reset: function() { | |
id = 0; | |
} | |
}; | |
})(globalThing, OtherGlobalThing); | |
there is a 4th option, you could just do | |
(function(window){ | |
// just explicitly say window.globalVariable inside of here for any global. | |
})(this); | |
or | |
var myModule = (function(globalThing, OtherGlobalThing) { | |
var privateVar = 0; | |
return { | |
next: function() { | |
return id++; | |
}, | |
reset: function() { | |
id = 0; | |
} | |
}; | |
})(this.globalThing, this.OtherGlobalThing); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment