Skip to content

Instantly share code, notes, and snippets.

@frostney
Created December 25, 2012 13:35
Show Gist options
  • Save frostney/4373262 to your computer and use it in GitHub Desktop.
Save frostney/4373262 to your computer and use it in GitHub Desktop.
Simple namespacing mechanism in CoffeeScript
namespace = (name, func) ->
if name.indexOf('.') > 0
nameArray = name.split('.')
else
nameArray = [name]
context = window
for i in nameArray
context = context[i] or= {}
func.call context
namespace 'a', ->
privateString = 'test'
@b = -> alert privateString
namespace 'a', ->
@c = -> alert 'oom!'
namespace 'a.sub', ->
@d = -> alert 'space!'
a.c()
a.sub.d()
a.b()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment