Skip to content

Instantly share code, notes, and snippets.

@StanAngeloff
Created December 7, 2010 14:06
Show Gist options
  • Save StanAngeloff/731816 to your computer and use it in GitHub Desktop.
Save StanAngeloff/731816 to your computer and use it in GitHub Desktop.
A quick and dirty way to use namespaces in CoffeeScript
# Code:
#
namespace = (target, name, block) ->
[target, name, block] = [exports ? window, arguments...] if arguments.length < 3
top = target
target = target[item] or= {} for item in name.split '.'
block target, top
# Usage:
#
namespace 'Hello.World', (exports) ->
# `exports` is where you attach namespace members
exports.hi = -> console.log 'Hi World!'
namespace 'Say.Hello', (exports, top) ->
# `top` is a reference to the main namespace
exports.fn = -> top.Hello.World.hi()
Say.Hello.fn() # prints 'Hi World!'
@StanAngeloff
Copy link
Author

@satyr: Yup, much better, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment