How to use namespaces within your JavaScript
- April 20, 2015
There are a couple of ways to create namespaces for your code. This format allows you to intentionally expose functions as public or keep them internal/private. I like the following version…
var mynamespace = new function () {
var internalFunction = function() { //TODO: code };
this.publicFunction = function() { //TODO: code };}; To execute the above public function from the global namespace would be done like mynamespace.publicFunction(); Obviously, you can use smaller/shorter names to make it easier to type. Simple, now why the heck do this?